C# png转ico
/// <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;
}
作者:重庆熊猫
出处:https://www.cnblogs.com/cqpanda/p/16928357.html
版权:本作品采用「不论是否商业使用都不允许转载,否则按3元1字进行收取费用」许可协议进行许可。
本文来自博客园,作者:重庆熊猫,转载请注明原文链接:https://www.cnblogs.com/cqpanda/p/16928357.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~