使用应用256×256 Vista的图标 [英] Using 256 x 256 Vista icon in application

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

问题描述

我有我已经做了256×256 Vista的图标的应用程序。

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

我不知道我怎么会是能够使用用作应用程序图标的ICO文件256x256的PNG,并显示在一个图片框窗体上。

I was wondering how I would be able to use the 256x256 png 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.

编辑!不知道这甚至有可能在XP中,可能需要Vista中的API

EDIT! Not sure if this is even possible in XP, may need vista APIs

感谢。

推荐答案

今天,我为做了一个很不错的功能的从Vista的图标提取256x256的位图。

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

和你一样,内森W,我用它来显示大图标,如关于框位图。例如,该code得到Vista的图标,PNG图像,并显示在256x256的图片框:

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

该函数将图标对象作为参数。所以,你可以用任何图标使用它 - 从资源,从文件,从流,等等。 (阅读下面有关提取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%托管code : - )

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()是很愚蠢的,它仅提取32×32的图标!

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!

相反,你最好使用全 IconExtractor 类,由津Kageyu(创建<一个href=\"http://www.$c$cproject.com/KB/cs/IconExtractor.aspx\">http://www.$c$cproject.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的图标指数,以获得所需Vista的图标。因此,使用我的ExtractVistaIcon()这里是更快,simplier方式:)

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×256 Vista的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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