class Program
{
static void Main(string[] args)
{
if (args.Length != 1)
return;
string imgName = args[0];
Console.WriteLine("开始处理...");
SaveToIcon(imgName);
Console.WriteLine("处理完毕...");
return ;
}
static void SaveToIcon(string srcImg)
{
Image img = Image.FromFile(srcImg);
using(MemoryStream msImg=new MemoryStream(),msIco=new MemoryStream())
{
img.Save(msImg, System.Drawing.Imaging.ImageFormat.Png);
using(var bin=new BinaryWriter(msIco))
{
bin.Write((short)0);
bin.Write((short)1);
bin.Write((short)1);
bin.Write((byte)img.Width);
bin.Write((byte)img.Height);
bin.Write((byte)0);
bin.Write((byte)0);
bin.Write((short)0);
bin.Write((short)32);
bin.Write((int)msImg.Length);
bin.Write(22);
bin.Write(msImg.ToArray());
bin.Flush();
bin.Seek(0, SeekOrigin.Begin);
Icon icon = new Icon(msIco);
string outputName = Path.Join(Path.GetDirectoryName(srcImg), Path.GetFileNameWithoutExtension(srcImg) + ".ico");
using (Stream stream = new FileStream(outputName, FileMode.Create))
{
icon.Save(stream);
}
}
}
}
}
原文:https://www.cnblogs.com/chyshx/p/15103248.html