Jimi2000

TreeView Run-Time ImagesList

Dec 13th, 2022 (edited)
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. //Set the StateImageList of a TreeView to a new ImageList, at run-time
  2. // imageList1 is used at design-time to setup the TreeView
  3. // Thes ImageList is supposed to have its ColorDepth set to 32 bit ARGB
  4. treeView1.StateImageList = TreeViewImageListRunTime(treeView1, imageList1);
  5.  
  6. // Dispose if the original ImageList is no longer needed
  7. imageList1.Dispose();
  8.  
  9. // [...]
  10.  
  11.         private ImageList TreeViewImageListRunTime(TreeView tv, ImageList imList)
  12.         {
  13.             ImageList cloned = imList.Container != null
  14.                 ? new ImageList(imList.Container) : new ImageList();
  15.            
  16.             // ColorDepth set to 8 bit indexed (could also be 32 bit)
  17.             cloned.ColorDepth = ColorDepth.Depth8Bit;
  18.             cloned.ImageSize = imList.ImageSize;
  19.             cloned.TransparentColor = tv.BackColor;
  20.  
  21.             for (int i = 0; i < imList.Images.Count; i++) {
  22.                 var bmp = new Bitmap(imList.ImageSize.Width, imList.ImageSize.Height);
  23.                 using (var g = Graphics.FromImage(bmp)) {
  24.                     g.FillRectangle(new SolidBrush(cloned.TransparentColor), 0, 0, bmp.Width, bmp.Height);
  25.  
  26.                     g.DrawImage(imList.Images[i], Point.Empty);
  27.                 }
  28.                 cloned.Images.Add(bmp);
  29.             }
  30.             return cloned;
  31.         }
  32.  
  33.  
Advertisement
Add Comment
Please, Sign In to add comment