Question ListView Items' Transparency Problem

aeskan

Well-known member
Joined
Aug 10, 2011
Messages
63
Programming Experience
3-5
Hi .Net folks

I have a ListView with a background image on my Form, which is related to an ImageList to show up pictures that exist in a folder as thumbnails. Everything works fine and ListView shows up thumbnails correctly. But when I try to use TransparentColor property of ImageList to remove unnecessary areas of thumbnails, everything disapears (I mean whole the thumbnail) except texts! Note that, I also used ListViewItem.BackColor = Color.Transparent and ListViewItem.UseItemStyleForSubItems and it did not change anything.

My code is as following and appreciate any help.


Dim ImageList1 As New ImageList()
ImageList1.ImageSize = New Size(160, 120)
ImageList1.ColorDepth = ColorDepth.Depth32Bit
ImageList1.TransparentColor = Color.White
'^This line causes thumbnails to completely disapear, otherwise everything is ok

ListView1.View =
View.LargeIcon
ListView1.SmallImageList = ImageList1
ListView1.LargeImageList = ImageList1
ListView1.Sorting = SortOrder.Ascending

Dim di As New DirectoryInfo(Path)
Dim Files() As FileInfo = GetFiles(di, "*.bmp;*.jpg;*.gif", SearchOption.TopDirectoryOnly)

If Files.Count > 0 Then
For Each File As FileInfo In Files
Try
picThumb1.Image = My.Resources.Empty1
'Note: My.Resources.Empty1 is a picture with a white background to make standard size thumbnails from multisize pictures
picThumb2.Image = Image.FromFile(File.FullName, False)
Dim g As Graphics = Graphics.FromImage(picThumb1.Image)
g.InterpolationMode =
InterpolationMode.HighQualityBilinear
g.DrawImage(picThumb2.Image, cLeft, cTop, cWidth, cHeight)
'Note: cLeft, cTop, cWidth, cHeight to be calculate elsewhere
g.Dispose()
ImageList1.Images.Add(picThumb1.Image)
ListView1.Items.Add(File.Name, ImageList1.Images.Count - 1)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
Next
End If

 
You draw on white, make white transparent, and are then surprised when you can't see it? What you're effectively doing is taking as transparent image, adding non-transparent information (the white background) and then asking the Image List to determine which transparent information it's supposed to be referring to with the inevitable result!

  'picThumb1.Image = My.Resources.Empty1
        'Note: My.Resources.Empty1 is a picture with a white background to make standard size thumbnails from multisize pictures


        'And that's why you have a problem. Don't draw on a surface at all but ...


        Dim bm As Bitmap = New Bitmap(cWidth, cHeight)
        Dim g As Graphics = Graphics.FromImage(bm)
        picThumb2.Image = Image.FromFile(File.FullName, False)
        'Dim g As Graphics = Graphics.FromImage(picThumb1.Image) - no!
        g.InterpolationMode = InterpolationMode.HighQualityBilinear
        g.DrawImage(picThumb2.Image, cLeft, cTop, cWidth, cHeight) 'as we are drawing to a new bitmap all the transparency information is retained
                                                                                             ' so there is no need to set any transparency colour in the image list
        'Note: cLeft, cTop, cWidth, cHeight to be calculate elsewhere
        g.Dispose()
        ImageList1.Images.Add(bm)
 
Thank you so much. As we sometimes draw on transparent papers with color pens, it really surprised me! Well it worls and thank you again for your useful words, but Dim bm As Bitmap = New Bitmap(cWidth, cHeight) needs Integer values whereas my calculated dimensions are Singles and it makes some problems in the thumbnails' insertion point and dimensions, even with CInt() and so on (I mean Top, Left, Width, Height). Seems I should work on it.
 
You draw on white, make white transparent, and are then surprised when you can't see it? What you're effectively doing is taking as transparent image, adding non-transparent information (the white background) and then asking the Image List to determine which transparent information it's supposed to be referring to with the inevitable result!

Respectfully it was all about saving the image to a temp file and loading it again to picbox before adding to ImageList, so we can draw on white and make white transparent without surprising.

People needs solutions not to sheer the problems :love-struck:. After all you made me to think more, best regards.
 
Last edited:
Back
Top