Link to home
Start Free TrialLog in
Avatar of mrflibbleuk
mrflibbleuk

asked on

Saving Extracted Icons Losing Transparency (Black)

I'm extracting the small icon from a .exe and attempting to save the image out as an icon file. However, the icon is always saving as black background, as if it has lost its transparency. I have had the same results using ExtractIconEx and SHGetFileInfo methods to obtain the icon.

If i convert the icon to a bitmap with: Dim x as Bitmap = myIcon.ToBitmap()  and save the resulting image to disk as bitmap, it looks fine. Any help would be appreciated, as I'm somewhat confused why this happening!
        Dim iconPtr As IntPtr
        Dim myIcon As Icon
        Dim numIcons As Integer
 
        numIcons = ExtractIconEx("c:\test\app.exe", 0, Nothing, iconPtr, 1)
        myIcon = Icon.FromHandle(iconPtr)
 
        Dim st As Stream = New FileStream("c:\test\app.ico", FileMode.Create)
        Dim wr As BinaryWriter = New BinaryWriter(st)
        myIcon.Save(st)
        wr.Close()
 
        DestroyIcon(iconPtr)

Open in new window

Avatar of PaulHews
PaulHews
Flag of Canada image

In framework 2.0 you can extract icons with ExtractAssociatedIcon instead of the API:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using myIcon As Icon = Icon.ExtractAssociatedIcon("C:\Temp\app.exe")
            Using fs As New FileStream("C:\temp\app.ico", FileMode.Create)
                myIcon.Save(fs)
            End Using
        End Using
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mrflibbleuk
mrflibbleuk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrflibbleuk
mrflibbleuk

ASKER

For 32 colour icons read 32 bit colour icons ...
mrflibbleuk,

That looks like great information.  I suggest you click the "Accept as Solution" link on your own comment above to begin the process of adding this question to the database of solutions.