wpf 如何从 png 图像中获取图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/384931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get an Icon from a png image?
提问by Morten Christiansen
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?
我正在创建一个 WPF 应用程序,所以我主要使用 ImageSource 类来处理图标。但是,系统托盘图标必须是 类型System.Drawing.Icon
。是否可以从 png 图像创建这样的对象?
I have tried the following:
我尝试了以下方法:
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;
}
}
The Icon constructor throws an exception with the following message: "Argument 'picture' must be a picture that can be used as a Icon."
Icon 构造函数抛出异常并显示以下消息:“参数‘图片’必须是可以用作图标的图片。”
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?
我认为这可能与图像颜色的位深度有关,因为我之前遇到了一些问题,但是 32 位和 24 位图像都不起作用。我正在尝试做的事情有可能吗?
采纳答案by maxnk
I think you can try something like this before convert your image to .ico:
我认为您可以在将图像转换为 .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.
哪里icon
将包含您需要的图标。
回答by Stephen Wrighton
There's also a website (http://www.convertico.com/) which converts PNGs into ICOs.
还有一个网站 ( http://www.convertico.com/) 可以将 PNG 转换为 ICO。
回答by Zhaph - Ben Duguid
Icons are a combination of 3 or 4 image sizes:
图标是 3 或 4 种图像尺寸的组合:
48 × 48, 32 × 32, 24 × 24 (optional), and 16 × 16 pixels.
48 × 48、32 × 32、24 × 24(可选)和 16 × 16 像素。
And can/should also contain three different colour depths:
并且可以/应该还包含三种不同的颜色深度:
- 24-bit with 8-bit alpha (32-bit)
- 8-bit (256 colors) with 1-bit transparency
- 4-bit (16 colors) with 1-bit transparency
- 24 位与 8 位 alpha(32 位)
- 8 位(256 色),1 位透明度
- 4 位(16 色),1 位透明度
So the .png memory stream isn't going to fit into the icon's constructor. In fact, if you read the notes on the other constructor overloads, you'll see all the "Size" or Width and Height measurements for finding the correct size icon in the file.
所以 .png 内存流不适合图标的构造函数。事实上,如果您阅读有关其他构造函数重载的注释,您将看到用于在文件中找到正确大小图标的所有“大小”或宽度和高度测量值。
More information on the manual creation of icons can be found under "Creating Windows XP Icons"
有关手动创建图标的更多信息,请参见“创建 Windows XP 图标”
回答by Vilx-
There is a .NET project called IconLib.
有一个名为IconLib的 .NET 项目。
public void Convert(string pngPath, string icoPath)
{
MultiIcon mIcon = new MultiIcon();
SingleIcon sIcon = mIcon.Add("Icon1");
sIcon.CreateFrom(pngPath, IconOutputFormat.FromWin95);
mIcon.SelectedIndex = 0;
mIcon.Save(icoPath, MultiIconFormat.ICO);
}
回答by Joee
Try this its working for me,
试试这个对我有用,
window.Icon = BitmapFrame.Create(Application.GetResourceStream(new Uri("YourImage.png",
UriKind.RelativeOrAbsolute)).Stream);
回答by devios1
You can set the ImageSource
of a window icon to a png image and it works, surprisingly. I haven't verified this for tray icons though.
您可以将ImageSource
窗口图标的 设置为 png 图像,它的工作原理令人惊讶。不过,我还没有针对托盘图标验证过这一点。