Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Set the StateImageList of a TreeView to a new ImageList, at run-time
- // imageList1 is used at design-time to setup the TreeView
- // Thes ImageList is supposed to have its ColorDepth set to 32 bit ARGB
- treeView1.StateImageList = TreeViewImageListRunTime(treeView1, imageList1);
- // Dispose if the original ImageList is no longer needed
- imageList1.Dispose();
- // [...]
- private ImageList TreeViewImageListRunTime(TreeView tv, ImageList imList)
- {
- ImageList cloned = imList.Container != null
- ? new ImageList(imList.Container) : new ImageList();
- // ColorDepth set to 8 bit indexed (could also be 32 bit)
- cloned.ColorDepth = ColorDepth.Depth8Bit;
- cloned.ImageSize = imList.ImageSize;
- cloned.TransparentColor = tv.BackColor;
- for (int i = 0; i < imList.Images.Count; i++) {
- var bmp = new Bitmap(imList.ImageSize.Width, imList.ImageSize.Height);
- using (var g = Graphics.FromImage(bmp)) {
- g.FillRectangle(new SolidBrush(cloned.TransparentColor), 0, 0, bmp.Width, bmp.Height);
- g.DrawImage(imList.Images[i], Point.Empty);
- }
- cloned.Images.Add(bmp);
- }
- return cloned;
- }
Advertisement
Add Comment
Please, Sign In to add comment