


Le contenu de cet article est de présenter qu'est-ce que la classe d'aide IO ? Exemple d'introduction de la classe d'assistance IO (avec code). Il a une certaine valeur de référence. Les amis dans le besoin peuvent s'y référer. J'espère qu'il vous sera utile.
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>序列对象 /// <param>XML文件路径 /// <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>目标类型(Type类型) /// <param>XML文件路径 /// <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>图片路径 /// <param>缩略图路径 /// <param>缩略图宽度 /// <param>缩略图高度 /// <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>源图路径 /// <param>水印图片路径 /// <param>保存路径 /// <param>位置 /// <param>透明度 /// <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 100) quality = 80; //水印透明度 float iii; if (opacity > 0 && opacity /// 生成文字水印 /// /// <param>源图路径 /// <param>保存路径 /// <param>水印文字 /// <param>文字大小 /// <param>文字字体 /// <param>位置 /// <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 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 } }
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

在使用Golang开发过程中,经常会遇到一些错误。其中之一是"undefined:io.ReadAll",这个错误大多数情况下是由于使用了过时的方法导致的。本文将会介绍如何解决这个错误。首先,让我们看一下发生这个错误的原因。在golang1.15版本之前,io包中并没有ReadAll方法。我们在使用这个方法时,编译器会提示“undefined:io.Re

概念fio,又称为FlexibleIOTester,是JensAxboe编写的应用程序。Jens是LinuxKernel中blockIOsubsystem的维护者。FIO是一种用于测试网络文件系统和磁盘性能的工具,常用于验证机型和比较文件系统性能。它能自动将fio命令发送到集群机器列表,并收集小文件的IOPS和大文件的吞吐量数据。rw=[mode]rwmixwrite=30在混合读写的模式下,写占30%moderead顺序读write顺序写readwrite顺序混合读写randwrite随机写r

使用io/ioutil.WriteFile函数将字符串写入文件,并设置文件权限和缩进格式在Go语言中,使用io/ioutil包中的WriteFile函数可以很方便地将字符串写入文件。同时,我们也可以通过设置文件权限和缩进格式来满足不同的需求。下面是一个示例代码,演示了如何使用WriteFile函数写入文件并设置权限和缩进格式:packagemainim

在使用golang进行开发时,我们经常会遇到各种各样的问题和错误提示。其中一个常见的问题就是出现了“undefined:io.TeeReader”错误。这个错误通常会在程序编译时出现,而且有时候可能会让人束手无策。本文将介绍如何解决这个问题,以便让您能够顺利地进行golang的开发工作。首先,让我们看一下这个错误的具体含义是什么。当我们在使用

linux io指的是一种文件操作;在Linux中,文件就是一串二进制流,那么在信息的交换过程中,我们都是对这些流进行数据收发操作,这些操作简称为I/O操作;由于Linux使用的是虚拟内存机制,所以必须通过系统调用请求内核来完成IO动作。

在Java编程中,常常需要对文件进行IO操作。文件IO操作涉及读写文件、创建目录、删除文件等操作。本文将介绍一些常用的文件IO操作技巧,以及如何在Java程序中使用它们。一、文件路径在Java中操作文件、目录,需要提供文件路径。文件路径可分为绝对路径和相对路径。绝对路径是文件在文件系统中的完整路径,以根目录开始。相对路径是文件相对于当前工作目录的路径。Jav

什么是Netty异步,基于事件驱动的网络应用框架,用以快速开发高性能,高可靠的网络IO程序主要针对在TCP协议下,面向Clients端的高并发应用本质是一个NIO框架,适用于服务器通讯等场景异步:发送请求无需等待响应,程式接着往下走。事件驱动:一个连接事件或者断开事件,或者读事件或者写事件,发生后的后续处理。Netty典型应用:高性能rpc框架用来远程服务(过程)调用,比如Dubbo。游戏行业,页面数据交互。大数据领域如Hadoop高性能通讯和序列化组件(AVRO)。IO模型简单理解就是用什么通

概念IO流可以初步的理解为数据间的传输,我们将一组数据入:1234567,将他们从hello文件中转入haha文件中,使用程序的方法进行转入的话则需要一个一个的传入,即为一个字节一个字节的传输,我们每次只能传入或读取一个字节,这就是io流的大致流程,io流对任何类型的文件都可以进行读取。如:文本文件,图片,歌曲mp3,视频等等的。因为io流是一个字节一个字节的传入读取的所以我们需要用到byte单字节变量来获取长度。如果获取过多的内容则需要使用对应的数组。io流对应的方法所有io流方法中都需要写入


Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

Adaptateur de serveur SAP NetWeaver pour Eclipse
Intégrez Eclipse au serveur d'applications SAP NetWeaver.

Version crackée d'EditPlus en chinois
Petite taille, coloration syntaxique, ne prend pas en charge la fonction d'invite de code

Dreamweaver Mac
Outils de développement Web visuel

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

VSCode Windows 64 bits Télécharger
Un éditeur IDE gratuit et puissant lancé par Microsoft
