Matt Wilko Matt Wilko - 6 months ago 29
Vb.net Question

How to use Semi-Transparent Images In an ImageList

I have an image that contains semi-transparent Images. I am then using this to bind to a ListView.

When I add the images to the ImageList (done at Runtime) the part that is transparent is turned grey.

As an example this shows the same image being loaded directly into a PictureBox and then via an ImageList into a PictureBox

The actual image is being created in memory, but this code gives the output as stated

Dim tempFilename As String = Path.GetTempFileName
Dim client As New WebClient()
client.DownloadFile("http://s32.postimg.org/k2fdrw3wh/Semi.png", tempFilename)
Dim empIcon = Image.FromFile(tempFilename)

PictureBox1.BackColor = Color.White
PictureBox1.Image = empIcon

PictureBox2.BackColor = Color.White
Dim imglst As New ImageList With {.ImageSize = New Size(32, 32), .ColorDepth = ColorDepth.Depth32Bit, .TransparentColor = Color.White}
imglst.Images.Add(empIcon)
PictureBox2.Image = imglst.Images(0)


PictureBox1 on the left 2 on the right:

enter image description here

How can I get the ImageList to output the image on PictureBox1?

KAI KAI
Answer

Get the transparency of the image by:

Dim transColor = CType(empIcon, Bitmap).GetPixel(1, 1)

and set it to the ImageList:

Dim imglst As New ImageList With {
    .ImageSize = New Size(32, 32),
    .ColorDepth = ColorDepth.Depth32Bit,
    .TransparentColor = transColor
}
Comments