在应用程序中使用256 x 256 Windows Vista图标 [英] Using a 256 x 256 Windows Vista icon in an application

查看:154
本文介绍了在应用程序中使用256 x 256 Windows Vista图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我已经制作了一个256 x 256 Windows的Vista图标。

I have an application which I have made a 256 x 256 Windows Vista icon for.

我想知道如何使用256x256 PNG文件在用作应用程序图标的ico文件中,并将其显示在表单上的图片框中。

I was wondering how I would be able to use a 256x256 PNG file in the ico file used as the application icon and show it in a picture box on a form.

我使用的是VB.NET,但C#中的答案很好。我想我可能不得不使用反射。

I am using VB.NET, but answers in C# are fine. I'm thinking I may have to use reflection.

我不确定这在Windows XP中是否可能,并且可能需要Windows  Vista API

I am not sure if this is even possible in Windows XP and may need Windows Vista APIs

推荐答案

今天,我为从Vista图标中提取256x256位图做了一个非常好的功能。

Today, I made a very nice function for extracting the 256x256 Bitmaps from Vista icons.

和你一样,Nathan W,我用它在关于框中将大图标显示为位图。例如,此代码将Vista图标作为PNG图像,并将其显示在256x256 PictureBox中:

Like you, Nathan W, I use it to display the large icon as a Bitmap in "About" box. For example, this code gets Vista icon as PNG image, and displays it in a 256x256 PictureBox:

picboxAppLogo.Image = ExtractVistaIcon(myIcon);

此函数将Icon对象作为参数。因此,您可以将它与任何图标一起使用 - 来自资源,来自文件,来自流等。 (请阅读以下有关提取EXE图标的信息)。

This function takes Icon object as a parameter. So, you can use it with any icons - from resources, from files, from streams, and so on. (Read below about extracting EXE icon).

它在任何操作系统上运行,因为它使用任何Win32 API,它是 100%托管代码: - )

It runs on any OS, because it does not use any Win32 API, it is 100% managed code :-)

// Based on: http://www.codeproject.com/KB/cs/IconExtractor.aspx
// And a hint from: http://www.codeproject.com/KB/cs/IconLib.aspx

Bitmap ExtractVistaIcon(Icon icoIcon)
{
    Bitmap bmpPngExtracted = null;
    try
    {
        byte[] srcBuf = null;
        using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
            { icoIcon.Save(stream); srcBuf = stream.ToArray(); }
        const int SizeICONDIR = 6;
        const int SizeICONDIRENTRY = 16;
        int iCount = BitConverter.ToInt16(srcBuf, 4);
        for (int iIndex=0; iIndex<iCount; iIndex++)
        {
            int iWidth  = srcBuf[SizeICONDIR + SizeICONDIRENTRY * iIndex];
            int iHeight = srcBuf[SizeICONDIR + SizeICONDIRENTRY * iIndex + 1];
            int iBitCount   = BitConverter.ToInt16(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 6);
            if (iWidth == 0 && iHeight == 0 && iBitCount == 32)
            {
                int iImageSize   = BitConverter.ToInt32(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 8);
                int iImageOffset = BitConverter.ToInt32(srcBuf, SizeICONDIR + SizeICONDIRENTRY * iIndex + 12);
                System.IO.MemoryStream destStream = new System.IO.MemoryStream();
                System.IO.BinaryWriter writer = new System.IO.BinaryWriter(destStream);
                writer.Write(srcBuf, iImageOffset, iImageSize);
                destStream.Seek(0, System.IO.SeekOrigin.Begin);
                bmpPngExtracted = new Bitmap(destStream); // This is PNG! :)
                break;
            }
        }
    }
    catch { return null; }
    return bmpPngExtracted;
}

重要!如果你想加载这个图标直接从EXE文件,然后你不能使用 Icon.ExtractAssociatedIcon(Application.ExecutablePath)作为参数,因为.NET函数ExtractAssociatedIcon()是如此愚蠢,它提取只有32x32图标!

IMPORTANT! If you want to load this icon directly from EXE file, then you CAN'T use Icon.ExtractAssociatedIcon(Application.ExecutablePath) as a parameter, because .NET function ExtractAssociatedIcon() is so stupid, it extracts ONLY 32x32 icon!

相反,你最好使用由Tsuda Kageyu创建的整个 IconExtractor 类( http://www.codeproject.com/KB/cs/IconExtractor.aspx )。您可以稍微简化此类,以使其更小。以这种方式使用 IconExtractor

Instead, you better use the whole IconExtractor class, created by Tsuda Kageyu (http://www.codeproject.com/KB/cs/IconExtractor.aspx). You can slightly simplify this class, to make it smaller. Use IconExtractor this way:

// Getting FILL icon set from EXE, and extracting 256x256 version for logo...
using (TKageyu.Utils.IconExtractor IconEx = new TKageyu.Utils.IconExtractor(Application.ExecutablePath))
{
    Icon icoAppIcon = IconEx.GetIcon(0); // Because standard System.Drawing.Icon.ExtractAssociatedIcon() returns ONLY 32x32.
    picboxAppLogo.Image = ExtractVistaIcon(icoAppIcon);
}

注意:我现在仍在使用ExtractVistaIcon()函数,因为我不喜欢 IconExtractor 如何处理这项工作 - 首先,它使用IconExtractor.SplitIcon(icoAppIcon)提取所有图标格式,然后您必须知道确切的256x256图标索引才能获得所需的视图 - 图标。因此,在这里使用我的ExtractVistaIcon()更加快捷和简单:)

Note: I'm still using my ExtractVistaIcon() function here, because I don't like how IconExtractor handles this job - first, it extracts all icon formats by using IconExtractor.SplitIcon(icoAppIcon), and then you have to know the exact 256x256 icon index to get the desired vista-icon. So, using my ExtractVistaIcon() here is much faster and simplier way :)

这篇关于在应用程序中使用256 x 256 Windows Vista图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆