如何从png图像中获取图标?

时间:2021-12-21 21:48:04

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位图像都不起作用。我有可能做的事情吗?

7 个解决方案

#1


17  

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.

图标将包含您需要的图标。

#2


8  

There's also a website (http://www.convertico.com/) which converts PNGs into ICOs.

还有一个网站(http://www.convertico.com/)将PNG转换为ICO。

#3


3  

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)
  • 24位,8位alpha(32位)
  • 8-bit (256 colors) with 1-bit transparency
  • 8位(256色),1位透明
  • 4-bit (16 colors) with 1-bit transparency
  • 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图标”

#4


3  

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);
}

#5


1  

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图像,这令人惊讶。我没有验证这个托盘图标。

#6


1  

Try this its working for me,

试试这个为我工作,

window.Icon = BitmapFrame.Create(Application.GetResourceStream(new Uri("YourImage.png",
UriKind.RelativeOrAbsolute)).Stream);

#7


0  

You could try a small command line app called png2ico. I use it to create Windows icons from pngs.

您可以尝试一个名为png2ico的小命令行应用程序。我用它来从pngs创建Windows图标。

#1


17  

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.

图标将包含您需要的图标。

#2


8  

There's also a website (http://www.convertico.com/) which converts PNGs into ICOs.

还有一个网站(http://www.convertico.com/)将PNG转换为ICO。

#3


3  

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)
  • 24位,8位alpha(32位)
  • 8-bit (256 colors) with 1-bit transparency
  • 8位(256色),1位透明
  • 4-bit (16 colors) with 1-bit transparency
  • 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图标”

#4


3  

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);
}

#5


1  

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图像,这令人惊讶。我没有验证这个托盘图标。

#6


1  

Try this its working for me,

试试这个为我工作,

window.Icon = BitmapFrame.Create(Application.GetResourceStream(new Uri("YourImage.png",
UriKind.RelativeOrAbsolute)).Stream);

#7


0  

You could try a small command line app called png2ico. I use it to create Windows icons from pngs.

您可以尝试一个名为png2ico的小命令行应用程序。我用它来从pngs创建Windows图标。