视频讲解地址 https://www.bilibili.com/video/BV1aU4y1S7rb
核心代码
/// <summary> /// 图片转换为ico文件 /// </summary> /// <param name="origin">原图片路径</param> /// <param name="destination">输出ico文件路径</param> /// <param name="iconSize">输出ico图标尺寸,不可大于255x255</param> /// <returns>是否转换成功</returns> public bool ConvertImageToIcon(string origin, string destination, Size iconSize) { if (iconSize.Width > 255 || iconSize.Height > 255) { return false; } Image image = new Bitmap(new Bitmap(origin), iconSize); //先读取已有的图片为bitmap,并缩放至设定大小 MemoryStream bitMapStream = new MemoryStream(); //存原图的内存流 MemoryStream iconStream = new MemoryStream(); //存图标的内存流 image.Save(bitMapStream, ImageFormat.Png); //将原图读取为png格式并存入原图内存流 BinaryWriter iconWriter = new BinaryWriter(iconStream); //新建二进制写入器以写入目标图标内存流 /** * 下面是根据原图信息,进行文件头写入 */ iconWriter.Write((short)0); iconWriter.Write((short)1); iconWriter.Write((short)1); iconWriter.Write((byte)image.Width); iconWriter.Write((byte)image.Height); iconWriter.Write((short)0); iconWriter.Write((short)0); iconWriter.Write((short)32); iconWriter.Write((int)bitMapStream.Length); iconWriter.Write(22); //写入图像体至目标图标内存流 iconWriter.Write(bitMapStream.ToArray()); //保存流,并将流指针定位至头部以Icon对象进行读取输出为文件 iconWriter.Flush(); iconWriter.Seek(0, SeekOrigin.Begin); Stream iconFileStream = new FileStream(destination, FileMode.Create); Icon icon = new Icon(iconStream); icon.Save(iconFileStream); //储存图像 /** * 下面开始释放资源 */ iconFileStream.Close(); iconWriter.Close(); iconStream.Close(); bitMapStream.Close(); icon.Dispose(); image.Dispose(); return File.Exists(destination); }
方法调用如下
bool b = ConvertImageToIcon(@"d:\1.png", @"d:\1.ico", new Size(64, 64));