diff --git a/Bin/SunnyUI.dll b/Bin/SunnyUI.dll index 7f884325..36b67429 100644 Binary files a/Bin/SunnyUI.dll and b/Bin/SunnyUI.dll differ diff --git a/Bin/SunnyUI.pdb b/Bin/SunnyUI.pdb index 5511d5c3..943ca4cb 100644 Binary files a/Bin/SunnyUI.pdb and b/Bin/SunnyUI.pdb differ diff --git a/SunnyUI.Demo/Bin/SunnyUI.dll b/SunnyUI.Demo/Bin/SunnyUI.dll index 7f884325..36b67429 100644 Binary files a/SunnyUI.Demo/Bin/SunnyUI.dll and b/SunnyUI.Demo/Bin/SunnyUI.dll differ diff --git a/SunnyUI/Static/UImage.cs b/SunnyUI/Static/UImage.cs index e47f3dd0..1a233038 100644 --- a/SunnyUI/Static/UImage.cs +++ b/SunnyUI/Static/UImage.cs @@ -567,5 +567,48 @@ namespace Sunny.UI return rotatedBmp; } + + /// + /// 转换Image为Icon + /// https://www.cnblogs.com/ahdung/p/ConvertToIcon.html + /// + /// 图片 + /// + public static Icon ToIcon(this Image image) + { + if (image == null) + { + return null; + } + + using (MemoryStream msImg = new MemoryStream(), msIco = new MemoryStream()) + { + image.Save(msImg, ImageFormat.Png); + + using (var bin = new BinaryWriter(msIco)) + { + //写图标头部 + bin.Write((short)0); //0-1保留 + bin.Write((short)1); //2-3文件类型。1=图标, 2=光标 + bin.Write((short)1); //4-5图像数量(图标可以包含多个图像) + + bin.Write((byte)image.Width); //6图标宽度 + bin.Write((byte)image.Height); //7图标高度 + bin.Write((byte)0); //8颜色数(若像素位深>=8,填0。这是显然的,达到8bpp的颜色数最少是256,byte不够表示) + bin.Write((byte)0); //9保留。必须为0 + bin.Write((short)0); //10-11调色板 + bin.Write((short)32); //12-13位深 + bin.Write((int)msImg.Length); //14-17位图数据大小 + bin.Write(22); //18-21位图数据起始字节 + + //写图像数据 + bin.Write(msImg.ToArray()); + + bin.Flush(); + bin.Seek(0, SeekOrigin.Begin); + return new Icon(msIco); + } + } + } } } \ No newline at end of file