ExtractAssociatedIcon Help!!!  
Author Message
Dominator Legend





PostPosted: Visual C# General, ExtractAssociatedIcon Help!!! Top

Hello,

I'm trying to extract icon from exe files during runtime, i know about the following function.


this.Icon = ico;

The problem is when i save the icon object to the disk!!

MemoryStream Memo = new MemoryStream();

ico .Save(Memo );

The above code should write the icon to C:\ the icon is saved but its deformed!!, i mean its colors looks very bad, can any one help, mention that the icon appear correctly in this line this.Icon = ico; but the saved icon appear very bad.

Please help, thanks




Visual C#20  
 
 
Dominator Legend





PostPosted: Visual C# General, ExtractAssociatedIcon Help!!! Top

Any help please.

Thanks.



 
 
Paul Louth





PostPosted: Visual C# General, ExtractAssociatedIcon Help!!! Top

Not sure why the icon isn't saving properly.  Does seem like a bug in the icon Save method.  But this works if you don't mind saving as a bitmap:



or use this class to extract the icon:

 

public class ExtractIcon
{
    [DllImport("Shell32.dll")]
    private static extern int SHGetFileInfo( string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbfileInfo, SHGFI uFlags );

    [StructLayout(LayoutKind.Sequential)]
    private struct SHFILEINFO
    {
        public SHFILEINFO(bool b)
        {
            hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
        }
        public IntPtr hIcon;
        public int iIcon;
        public uint dwAttributes;

        [MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
        public string szDisplayName;

        [MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
        public string szTypeName;
    };

    private ExtractIcon()
    {
    }

    private enum SHGFI
    {
        SmallIcon = 0x00000001,
        LargeIcon = 0x00000000,
        Icon = 0x00000100,
        DisplayName = 0x00000200,
        Typename = 0x00000400,
        SysIconIndex = 0x00004000,
        UseFileAttributes = 0x00000010
    }

    public static Icon GetIcon(string strPath, bool bSmall)
    {
        SHFILEINFO info = new SHFILEINFO(true);
        int cbFileInfo = Marshal.SizeOf(info);

        SHGFI flags;
        if (bSmall)
        {
            flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;
        }
        else
        {
            flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes;
        }

        SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);

        return Icon.FromHandle(info.hIcon);
    }
}


 
 
Mark Benningfield





PostPosted: Visual C# General, ExtractAssociatedIcon Help!!! Top

Hello All.

Dominator Legend:

Check out this article. It was linked in this thread on the forums. Explains what the problem is and how to fix it.

HTH.



 
 
Dominator Legend





PostPosted: Visual C# General, ExtractAssociatedIcon Help!!! Top

Thanks a lot Mark,

Extracting Icon using API is long process, but its the only way to extract good quality icon.

I think there is a bug in Icon class. thanks for your link.



 
 
Dominator Legend





PostPosted: Visual C# General, ExtractAssociatedIcon Help!!! Top

Thanks Paul for your replay, the problem is not how to get the icon your class get the icon, the problem how to save that icon to the disk, the .Save function deforme the icon,

Any way thanks a lot for your post :).

Chear.



 
 
Paul Louth





PostPosted: Visual C# General, ExtractAssociatedIcon Help!!! Top

Thanks Paul for your replay, the problem is not how to get the icon your class get the icon, the problem how to save that icon to the disk, the .Save function deforme the icon,

Any way thanks a lot for your post :).

Chear.

No problem, was the first part not useful either Or did you need it in the ico format


 
 
Dominator Legend





PostPosted: Visual C# General, ExtractAssociatedIcon Help!!! Top

I'm building an app. that take as an input exe file and encapsulate the input file into new created exe file, the new exe file should have the same icon as the input, i try to get the icon, and then convert it to Bmp, and save the Bmp file with Ico format, the icon extracted correctly and colors was good, but the problem is that its not a standard icon which mean when i try to open it using any icon editor it can't recognize it, but the shell render it correctly, i don't know what is wrong with these classes.

Any way thanks again for your posts :)



 
 
Paul Louth





PostPosted: Visual C# General, ExtractAssociatedIcon Help!!! Top

Yeah the icon file-format has always seemed a bit of a black-sheep of the image file-format family!