.net’s Treeview control has a sticky issue when using transparent images via ImageList. Solved…
I was noticing crappy rendering when using the standard ImageList <> TreeView pairing with transparent images. An example
You’ll notice the poor alpha blending around the edges of the icons. It transpires this is a persisting ‘feature’ of the windows core. See here
The way to solve this is to produce image that pull on the treeview control’s background color to produce a new image list from an array of images with transparencies (essentially producing a solid image, but with the TV’s background color so they appear transparent). Some example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Private Sub ConstructImageList(_imageArray) ' _imageArray = List(Of Image) ' TreeView = name of a TreeView control Dim ReRenederedImageList = New ImageList For Each image As Image In _imageArray Dim bitmap As Bitmap = New Bitmap(image.Width, image.Height) Using g = Graphics.FromImage(bitmap) g.Clear(TreeView.BackColor) ' this sets the bg color of the image to that of the treeview g.DrawImage(image, 0, 0) End Using ReRenederedImageList.Images.Add(bitmap) Next TreeView.ImageList = ReRenederedImageList End Sub |
Results in:
Alternatively, just build a helper class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Public Class ImageOp Public Shared Function CompensateForBackgroundColor(image As Image, backcolor As Color) As Image Dim bitmap As Bitmap = New Bitmap(image.Width, image.Height) Using g = Graphics.FromImage(bitmap) g.Clear(backcolor) ' this sets the bg color of the image to that of the treeview g.DrawImage(image, 0, 0) End Using Return bitmap End Function End Class |
Leave a Reply