本篇文章给大家带来的内容是介绍IO帮助类是什么?IO帮助类的实例介绍(附代码)。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。
using System; using System.IO; using System.Web; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Xml.Serialization; namespace ZMM.Core { /// <summary> /// IO帮助类 /// </summary> public class IOHelper { //是否已经加载了JPEG编码解码器 private static bool _isloadjpegcodec = false; //当前系统安装的JPEG编码解码器 private static ImageCodecInfo _jpegcodec = null; /// <summary> /// 获得文件物理路径 /// </summary> /// <returns></returns> public static string GetMapPath(string path) { if (HttpContext.Current != null) { return HttpContext.Current.Server.MapPath(path); } else { return System.Web.Hosting.HostingEnvironment.MapPath(path); } } #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="obj">序列对象</param> /// <param name="filePath">XML文件路径</param> /// <returns>是否成功</returns> public static bool SerializeToXml(object obj, string filePath) { bool result = false; FileStream fs = null; try { fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); XmlSerializer serializer = new XmlSerializer(obj.GetType()); serializer.Serialize(fs, obj); result = true; } catch (Exception ex) { throw ex; } finally { if (fs != null) fs.Close(); } return result; } /// <summary> /// XML反序列化 /// </summary> /// <param name="type">目标类型(Type类型)</param> /// <param name="filePath">XML文件路径</param> /// <returns>序列对象</returns> public static object DeserializeFromXML(Type type, string filePath) { FileStream fs = null; try { fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); XmlSerializer serializer = new XmlSerializer(type); return serializer.Deserialize(fs); } catch (Exception ex) { throw ex; } finally { if (fs != null) fs.Close(); } } #endregion #region 水印,缩略图 /// <summary> /// 获得当前系统安装的JPEG编码解码器 /// </summary> /// <returns></returns> public static ImageCodecInfo GetJPEGCodec() { if (_isloadjpegcodec == true) return _jpegcodec; ImageCodecInfo[] codecsList = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo codec in codecsList) { if (codec.MimeType.IndexOf("jpeg") > -1) { _jpegcodec = codec; break; } } _isloadjpegcodec = true; return _jpegcodec; } /// <summary> /// 生成缩略图 /// </summary> /// <param name="imagePath">图片路径</param> /// <param name="thumbPath">缩略图路径</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void GenerateThumb(string imagePath, string thumbPath, int width, int height, string mode) { Image image = Image.FromFile(imagePath); string extension = imagePath.Substring(imagePath.LastIndexOf(".")).ToLower(); ImageFormat imageFormat = null; switch (extension) { case ".jpg": case ".jpeg": imageFormat = ImageFormat.Jpeg; break; case ".bmp": imageFormat = ImageFormat.Bmp; break; case ".png": imageFormat = ImageFormat.Png; break; case ".gif": imageFormat = ImageFormat.Gif; break; default: imageFormat = ImageFormat.Jpeg; break; } int toWidth = width > 0 ? width : image.Width; int toHeight = height > 0 ? height : image.Height; int x = 0; int y = 0; int ow = image.Width; int oh = image.Height; switch (mode) { case "HW"://指定高宽缩放(可能变形) break; case "W"://指定宽,高按比例 toHeight = image.Height * width / image.Width; break; case "H"://指定高,宽按比例 toWidth = image.Width * height / image.Height; break; case "Cut"://指定高宽裁减(不变形) if ((double)image.Width / (double)image.Height > (double)toWidth / (double)toHeight) { oh = image.Height; ow = image.Height * toWidth / toHeight; y = 0; x = (image.Width - ow) / 2; } else { ow = image.Width; oh = image.Width * height / toWidth; x = 0; y = (image.Height - oh) / 2; } break; default: break; } //新建一个bmp Image bitmap = new Bitmap(toWidth, toHeight); //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(image, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { bitmap.Save(thumbPath, imageFormat); } catch (Exception ex) { throw ex; } finally { if (g != null) g.Dispose(); if (bitmap != null) bitmap.Dispose(); if (image != null) image.Dispose(); } } /// <summary> /// 生成图片水印 /// </summary> /// <param name="originalPath">源图路径</param> /// <param name="watermarkPath">水印图片路径</param> /// <param name="targetPath">保存路径</param> /// <param name="position">位置</param> /// <param name="opacity">透明度</param> /// <param name="quality">质量</param> public static void GenerateImageWatermark(string originalPath, string watermarkPath, string targetPath, int position, int opacity, int quality) { Image originalImage = null; Image watermarkImage = null; //图片属性 ImageAttributes attributes = null; //画板 Graphics g = null; try { originalImage = Image.FromFile(originalPath); watermarkImage = new Bitmap(watermarkPath); if (watermarkImage.Height >= originalImage.Height || watermarkImage.Width >= originalImage.Width) { originalImage.Save(targetPath); return; } if (quality < 0 || quality > 100) quality = 80; //水印透明度 float iii; if (opacity > 0 && opacity <= 10) iii = (float)(opacity / 10.0F); else iii = 0.5F; //水印位置 int x = 0; int y = 0; switch (position) { case 1: x = (int)(originalImage.Width * (float).01); y = (int)(originalImage.Height * (float).01); break; case 2: x = (int)((originalImage.Width * (float).50) - (watermarkImage.Width / 2)); y = (int)(originalImage.Height * (float).01); break; case 3: x = (int)((originalImage.Width * (float).99) - (watermarkImage.Width)); y = (int)(originalImage.Height * (float).01); break; case 4: x = (int)(originalImage.Width * (float).01); y = (int)((originalImage.Height * (float).50) - (watermarkImage.Height / 2)); break; case 5: x = (int)((originalImage.Width * (float).50) - (watermarkImage.Width / 2)); y = (int)((originalImage.Height * (float).50) - (watermarkImage.Height / 2)); break; case 6: x = (int)((originalImage.Width * (float).99) - (watermarkImage.Width)); y = (int)((originalImage.Height * (float).50) - (watermarkImage.Height / 2)); break; case 7: x = (int)(originalImage.Width * (float).01); y = (int)((originalImage.Height * (float).99) - watermarkImage.Height); break; case 8: x = (int)((originalImage.Width * (float).50) - (watermarkImage.Width / 2)); y = (int)((originalImage.Height * (float).99) - watermarkImage.Height); break; case 9: x = (int)((originalImage.Width * (float).99) - (watermarkImage.Width)); y = (int)((originalImage.Height * (float).99) - watermarkImage.Height); break; } //颜色映射表 ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] newColorMap = { colorMap }; //颜色变换矩阵,iii是设置透明度的范围0到1中的单精度类型 float[][] newColorMatrix ={ new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, iii, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; //定义一个 5 x 5 矩阵 ColorMatrix matrix = new ColorMatrix(newColorMatrix); //图片属性 attributes = new ImageAttributes(); attributes.SetRemapTable(newColorMap, ColorAdjustType.Bitmap); attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //画板 g = Graphics.FromImage(originalImage); //绘制水印 g.DrawImage(watermarkImage, new Rectangle(x, y, watermarkImage.Width, watermarkImage.Height), 0, 0, watermarkImage.Width, watermarkImage.Height, GraphicsUnit.Pixel, attributes); //保存图片 EncoderParameters encoderParams = new EncoderParameters(); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, new long[] { quality }); if (GetJPEGCodec() != null) originalImage.Save(targetPath, _jpegcodec, encoderParams); else originalImage.Save(targetPath); } catch (Exception ex) { throw ex; } finally { if (g != null) g.Dispose(); if (attributes != null) attributes.Dispose(); if (watermarkImage != null) watermarkImage.Dispose(); if (originalImage != null) originalImage.Dispose(); } } /// <summary> /// 生成文字水印 /// </summary> /// <param name="originalPath">源图路径</param> /// <param name="targetPath">保存路径</param> /// <param name="text">水印文字</param> /// <param name="textSize">文字大小</param> /// <param name="textFont">文字字体</param> /// <param name="position">位置</param> /// <param name="quality">质量</param> public static void GenerateTextWatermark(string originalPath, string targetPath, string text, int textSize, string textFont, int position, int quality) { Image originalImage = null; //画板 Graphics g = null; try { originalImage = Image.FromFile(originalPath); //画板 g = Graphics.FromImage(originalImage); if (quality < 0 || quality > 100) quality = 80; Font font = new Font(textFont, textSize, FontStyle.Regular, GraphicsUnit.Pixel); SizeF sizePair = g.MeasureString(text, font); float x = 0; float y = 0; switch (position) { case 1: x = (float)originalImage.Width * (float).01; y = (float)originalImage.Height * (float).01; break; case 2: x = ((float)originalImage.Width * (float).50) - (sizePair.Width / 2); y = (float)originalImage.Height * (float).01; break; case 3: x = ((float)originalImage.Width * (float).99) - sizePair.Width; y = (float)originalImage.Height * (float).01; break; case 4: x = (float)originalImage.Width * (float).01; y = ((float)originalImage.Height * (float).50) - (sizePair.Height / 2); break; case 5: x = ((float)originalImage.Width * (float).50) - (sizePair.Width / 2); y = ((float)originalImage.Height * (float).50) - (sizePair.Height / 2); break; case 6: x = ((float)originalImage.Width * (float).99) - sizePair.Width; y = ((float)originalImage.Height * (float).50) - (sizePair.Height / 2); break; case 7: x = (float)originalImage.Width * (float).01; y = ((float)originalImage.Height * (float).99) - sizePair.Height; break; case 8: x = ((float)originalImage.Width * (float).50) - (sizePair.Width / 2); y = ((float)originalImage.Height * (float).99) - sizePair.Height; break; case 9: x = ((float)originalImage.Width * (float).99) - sizePair.Width; y = ((float)originalImage.Height * (float).99) - sizePair.Height; break; } g.DrawString(text, font, new SolidBrush(Color.White), x + 1, y + 1); g.DrawString(text, font, new SolidBrush(Color.Black), x, y); //保存图片 EncoderParameters encoderParams = new EncoderParameters(); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, new long[] { quality }); if (GetJPEGCodec() != null) originalImage.Save(targetPath, _jpegcodec, encoderParams); else originalImage.Save(targetPath); } catch (Exception ex) { throw ex; } finally { if (g != null) g.Dispose(); if (originalImage != null) originalImage.Dispose(); } } #endregion } }
以上是IO帮助类是什么?IO帮助类的实例介绍(附代码)的详细内容。更多信息请关注PHP中文网其他相关文章!

C#.NET开发者社区提供了丰富的资源和支持,包括:1.微软的官方文档,2.社区论坛如StackOverflow和Reddit,3.GitHub上的开源项目,这些资源帮助开发者从基础学习到高级应用,提升编程技能。

C#.NET的优势包括:1)语言特性,如异步编程简化了开发;2)性能与可靠性,通过JIT编译和垃圾回收机制提升效率;3)跨平台支持,.NETCore扩展了应用场景;4)实际应用广泛,从Web到桌面和游戏开发都有出色表现。

C#并不总是与.NET捆绑在一起。1)C#可以在Mono运行时环境中运行,适用于Linux和macOS。2)在Unity游戏引擎中,C#用于脚本编写,不依赖.NET框架。3)C#还可用于嵌入式系统开发,如.NETMicroFramework。

C#在.NET生态系统中扮演核心角色,是开发者的首选语言。1)C#提供高效、易用的编程方式,结合C、C 和Java的优点。2)通过.NET运行时(CLR)执行,确保跨平台高效运行。3)C#支持从基本到高级的用法,如LINQ和异步编程。4)优化和最佳实践包括使用StringBuilder和异步编程,提高性能和可维护性。

C#是微软在2000年发布的编程语言,旨在结合C 的强大功能和Java的简洁性。1.C#是一种类型安全、面向对象的编程语言,支持封装、继承和多态。2.C#的编译过程将代码转化为中间语言(IL),然后在.NET运行时环境(CLR)中即时编译成机器码执行。3.C#的基本用法包括变量声明、控制流和函数定义,而高级用法涵盖异步编程、LINQ和委托等。4.常见错误包括类型不匹配和空引用异常,可通过调试器、异常处理和日志记录来调试。5.性能优化建议包括使用LINQ、异步编程和提高代码可读性。

C#是一种编程语言,而.NET是一个软件框架。1.C#由微软开发,适用于多平台开发。2..NET提供类库和运行时环境,支持多语言。两者协同工作,构建现代应用。

C#.NET是一个强大的开发平台,结合了C#语言和.NET框架的优势。1)它广泛应用于企业应用、Web开发、游戏开发和移动应用开发。2)C#代码编译成中间语言后由.NET运行时环境执行,支持垃圾回收、类型安全和LINQ查询。3)使用示例包括基本控制台输出和高级LINQ查询。4)常见错误如空引用和类型转换错误可以通过调试器和日志记录解决。5)性能优化建议包括异步编程和优化LINQ查询。6)尽管面临竞争,C#.NET通过不断创新保持其重要地位。

C#.NET的未来趋势主要集中在云计算、微服务、AI和机器学习集成以及跨平台开发三个方面。1)云计算和微服务:C#.NET通过Azure平台优化云环境表现,支持构建高效微服务架构。2)AI和机器学习集成:借助ML.NET库,C#开发者可在应用中嵌入机器学习模型,推动智能化应用发展。3)跨平台开发:通过.NETCore和.NET5 ,C#应用可在Windows、Linux和macOS上运行,扩展部署范围。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

禅工作室 13.0.1
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境