c#图片转ico图标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | /// <summary> /// Converts a PNG image to a icon (ico) /// </summary> /// <param name="inputPath">The input path</param> /// <param name="outputPath">The output path</param> /// <param name="size">The size (16x16 px by default)</param> /// <param name="preserveAspectRatio">Preserve the aspect ratio</param> /// <returns>Wether or not the icon was succesfully generated</returns> public static bool ConvertPNGToIcon( string inputPath, string outputPath, int size = 256, bool preserveAspectRatio = true ) { using (FileStream inputStream = new FileStream(inputPath, FileMode.Open)) using (FileStream outputStream = new FileStream(outputPath, FileMode.OpenOrCreate)) { return ConvertToIcon(inputStream, outputStream, size, preserveAspectRatio); } } /// <summary> /// Converts a PNG image to a icon (ico) /// </summary> /// <param name="input">The input stream</param> /// <param name="output">The output stream</param> /// <param name="size">The size (16x16 px by default)</param> /// <param name="preserveAspectRatio">Preserve the aspect ratio</param> /// <returns>Wether or not the icon was succesfully generated</returns> public static bool ConvertPNGToIcon(Stream input, Stream output, int size = 16, bool preserveAspectRatio = false ) { Bitmap inputBitmap = (Bitmap)Bitmap.FromStream(input); if (inputBitmap != null ) { int width, height; if (preserveAspectRatio) { width = size; height = inputBitmap.Height / inputBitmap.Width * size; } else { width = height = size; } Bitmap newBitmap = new Bitmap(inputBitmap, new System.Drawing.Size(width, height)); if (newBitmap != null ) { // save the resized png into a memory stream for future use using (MemoryStream memoryStream = new MemoryStream()) { newBitmap.Save(memoryStream, ImageFormat.Png); BinaryWriter iconWriter = new BinaryWriter(output); if (output != null && iconWriter != null ) { // 0-1 reserved, 0 iconWriter.Write(( byte )0); iconWriter.Write(( byte )0); // 2-3 image type, 1 = icon, 2 = cursor iconWriter.Write(( short )1); // 4-5 number of images iconWriter.Write(( short )1); // image entry 1 // 0 image width iconWriter.Write(( byte )width); // 1 image height iconWriter.Write(( byte )height); // 2 number of colors iconWriter.Write(( byte )0); // 3 reserved iconWriter.Write(( byte )0); // 4-5 color planes iconWriter.Write(( short )0); // 6-7 bits per pixel iconWriter.Write(( short )32); // 8-11 size of image data iconWriter.Write(( int )memoryStream.Length); // 12-15 offset of image data iconWriter.Write(( int )(6 + 16)); // write image data // png data must contain the whole png data file iconWriter.Write(memoryStream.ToArray()); iconWriter.Flush(); return true ; } } } return false ; } return false ; } 调用: string sourceurl = @"C:\Users\wh\Desktop\摄像头1-01.png" ; string outUrl = @"C:\Users\wh\Desktop\pt.ico" ; bool bl = FileHelper.ConvertPNGToIcon(sourceurl, outUrl); |
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利
如有需要可联系(qq:2831804923 / 微信号:wh0612910)
----------------------------------------------------------------------------------------------------------------------
愿本文对阁下有帮助,在下谢之:
如有需要可联系(qq:2831804923 / 微信号:wh0612910)
----------------------------------------------------------------------------------------------------------------------
愿本文对阁下有帮助,在下谢之:


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)