Image from an array of bytes

I need to make an image in a column ObjectListView, so I set ImageRendererthat takes an array of bytes and uses this code to change it to an image

image = Image.FromStream(stream);

However, I need to extract the icon from the exe file, convert it to an array of bytes so that it ObjectListViewcan display it.
Here is the code I'm using:

Using ms = New MemoryStream()
    Dim imageIn = Icon.ExtractAssociatedIcon(exe_path)
    imageIn.Save(ms)
    Return ms.ToArray()
End Using

The problem is that the image is displayed with the wrong colors (for example, if it was 8bpp).
So I tried using this code to find the problem:

Using ms = New MemoryStream()
    Dim imageIn = Icon.ExtractAssociatedIcon(exe_path)
    imageIn.Save(ms)

    Dim bmp = imageIn.ToBitmap()
    bmp.Save("img1.bmp")
    Using mt As New MemoryStream(ms.ToArray())
        Dim img = Image.FromStream(mt)
        img.Save("img2.bmp")
    End Using
End Using

In this case, it img1.bmpis correct (a raster image with real colors), but img2.bmphas the wrong colors; therefore either ms.ToArray()or Image.FromStreamdamages the image.

:
, , , "".
- PNG

Using ms = New MemoryStream()
    Dim bmp = Icon.ExtractAssociatedIcon(exe_path).ToBitmap()
    bmp.MakeTransparent(bmp.GetPixel(0, 0))
    bmp.Save(ms, ImageFormat.Png)

    Using mt As New MemoryStream(ms.ToArray())
        Dim img = Image.FromStream(mt)
        img.Save("img2.bmp")
    End Using
End Using
+3
1

, Icon , Image. Icon , Image. Icon , Image . , Icon - (), Image .

Icon , , :

Using ms = New MemoryStream()
    Dim imageIn = Icon.ExtractAssociatedIcon(exe_path)
    imageIn.ToBitmap().Save(ms, ImageFormat.Bmp)
    Return ms.ToArray()
End Using
+2

All Articles