C#实战小技巧(十二):获取本地应用程序图标

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WZh0316/article/details/86356307

使用.NET 4.0编程时,可以使用System.Drawing.Icon类的静态方法ExtractAssociatedIcon获取本地应用程序的图标。下方是示例代码,address变量时本地应用程序地址,获取图标后转为base64,保存在info对象的functionIcon属性中。
需要注意,要将获取的图标转为Bitmap的PNG格式(保存为PNG格式可保持原图透明度),不能直接保存为Icon,因为.NET的Icon图片默认只有16色,保存为Icon会导致图片失真。

					using (Image appIcon = Icon.ExtractAssociatedIcon(address).ToBitmap())
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            appIcon.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                            byte[] arr = new byte[ms.Length];
                            ms.Position = 0;
                            ms.Read(arr, 0, (int)ms.Length);
                            info.functionIcon = Convert.ToBase64String(arr).Trim();
                        }
                    }

猜你喜欢

转载自blog.csdn.net/WZh0316/article/details/86356307
今日推荐