如何从png图像中获取图标? [英] How do I get an Icon from a png image?
问题描述
我正在创建一个WPF应用程序,因此我主要使用ImageSource类来处理图标。但是,系统托盘图标必须是 System.Drawing.Icon
类型。是否可以从png图像创建这样的对象?
I'm creating an WPF app, so I'm mostly working with the ImageSource class for icons. However, the system tray icon has to be of type System.Drawing.Icon
. Is it possible to create such an object from a png image?
我尝试过以下内容:
private static System.Drawing.Icon _pngIcon;
public static System.Drawing.Icon PngIcon
{
get
{
if (_pngIcon == null)
{
//16x16 png image (24 bit or 32bit color)
System.Drawing.Bitmap icon = global::BookyPresentation.Properties.Resources.star16;
MemoryStream iconStream = new MemoryStream();
icon.Save(iconStream, System.Drawing.Imaging.ImageFormat.Png);
iconStream.Seek(0, SeekOrigin.Begin);
_pngIcon = new System.Drawing.Icon(iconStream); //Throws exception
}
return _pngIcon;
}
}
Icon构造函数抛出异常,并显示以下消息: 参数'图片'必须是可以用作图标的图片。
The Icon constructor throws an exception with the following message: "Argument 'picture' must be a picture that can be used as a Icon."
我认为它可能是像我一样的图像颜色的位深度之前的一些问题,但32位和24位图像都不起作用。有可能我正在尝试做什么吗?
I figured it might be something with the bit depth of the image color as I had some issues with this earlier, but both 32bit and 24bit images didn't work. Is it possible what I'm trying to do?
推荐答案
我认为你可以在转换图像之前尝试这样的事情。 ico:
I think you can try something like this before convert your image to .ico:
var bitmap = new Bitmap("Untitled.png"); // or get it from resource
var iconHandle = bitmap.GetHicon();
var icon = System.Drawing.Icon.FromHandle(iconHandle);
其中图标
将包含你的图标需要。
Where icon
will contain the icon which you need.
这篇关于如何从png图像中获取图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!