本篇文章帶給大家的內容是介紹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>序列对象 /// <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 } }
以上是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通過不斷創新保持其重要地位。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器