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

发布于 2024-07-09 22:20:21 字数 896 浏览 8 评论 0 原文

我正在创建一个 WPF 应用程序,因此我主要使用图标的 ImageSource 类。 但是,系统托盘图标必须是 System.Drawing.Icon 类型。 是否可以从 png 图像创建这样的对象?

我已尝试以下操作:

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 构造函数抛出异常并显示以下消息:“参数‘图片’必须是可用作图标的图片。”

我认为这可能与图像颜色的位深度有关,因为我之前遇到了一些问题,但 32 位和 24 位图像都不起作用。 我想做的事情可能吗?

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?

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."

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

脸赞 2024-07-16 22:20:21

我认为您可以在将图像转换为 .ico 之前尝试类似的操作:

    var bitmap = new Bitmap("Untitled.png"); // or get it from resource
    var iconHandle = bitmap.GetHicon();
    var icon = System.Drawing.Icon.FromHandle(iconHandle);

其中 icon 将包含您需要的图标。

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.

看春风乍起 2024-07-16 22:20:21

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

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

陌若浮生 2024-07-16 22:20:21

图标是 3 或 4 种图像尺寸的组合:

48 × 48、32 × 32、24 × 24(可选)和 16 × 16 像素。

并且还可以/应该包含三种不同的颜色深度:

  • 24 位,带 8 位 alpha(32 位)
  • 8 位(256 色),带 1 位透明度
  • 4 位(16 色),带 1 位透明度

所以.png 内存流不适合图标的构造函数。 事实上,如果您阅读其他构造函数重载的注释,您将看到所有“尺寸”或宽度和高度测量值,以便在文件中查找正确尺寸的图标。

有关手动创建图标的更多信息,请参阅“创建 Windows XP 图标

Icons are a combination of 3 or 4 image sizes:

48 × 48, 32 × 32, 24 × 24 (optional), and 16 × 16 pixels.

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

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.

More information on the manual creation of icons can be found under "Creating Windows XP Icons"

A君 2024-07-16 22:20:21

有一个名为 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);
}

There is a .NET project called IconLib.

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);
}
黯然#的苍凉 2024-07-16 22:20:21

试试这个对我有用,

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

Try this its working for me,

window.Icon = BitmapFrame.Create(Application.GetResourceStream(new Uri("YourImage.png",
UriKind.RelativeOrAbsolute)).Stream);
两相知 2024-07-16 22:20:21

您可以将窗口图标的 ImageSource 设置为 png 图像,并且效果令人惊讶。 不过我还没有验证托盘图标的这一点。

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.

憧憬巴黎街头的黎明 2024-07-16 22:20:21

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文