Using a 256 x 256 Windows Vista icon in an application

前端 未结 5 1156
离开以前
离开以前 2020-12-08 09:03

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

I was wondering how I would be able to use a 256x256 PNG file in the ico file used a

5条回答
  •  感情败类
    2020-12-08 09:13

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

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

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

    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

    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!

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

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

提交回复
热议问题