32-bit images in ImageList

I have the following image:

Icon

I read it from resources by putting it in an ImageList and then read it from an ImageList to draw on my control surface. But when I do this, the image seems to lose alpha channel information:

Screen shot

Here are all the relevant code snippets:

Program.cs

Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); 

MainForm.cs

 ilFilters = new ImageList() { ColorDepth = ColorDepth.Depth32Bit, ImageSize = new Size(16, 16) }; // (...) if (info.Icon != null) { if (info.Icon.Width == 16 && info.Icon.Height == 16) { ilFilters.Images.Add(info.Icon); index = ilFilters.Images.Count - 1; } } 

I actually saved the image before ( info.Icon ) and after ( ilFilters.Images[0] ) put it in the list of images. The first version was correct, the second was damaged. ImageList seems to have damaged the image.

I also tried converting PNG to a 32-bit BMP image. No success.

On the system, I am running Windows 7..NET 4.5.1, the Visual Studio 2013 community.

How can I save the alpha channel of an image when added to an imagelist?


Edit: Proof of conceptual application .

+2
source share
1 answer

From the proof of concept you published, I figured out a simple way to make it work. Basically, you should define ImageList in the WinForms designer.

To give a complete answer, here are all the steps I took to get it working:

  • In the view of the Form constructor, create an ImageList from the ToolBox.
  • In the designer, set ColorDepth to "Depth32Bit", the correct image size, and TransparentColor to "Transparent."
  • After InitializeComponent() add your images

Like this:

 ImageList1.Images.AddRange(new[] { Resources.IconPlay, Resources.IconPause, Resources.IconStop, [...] }); 
  1. Assign ImageList to TreeView in the constructor.
  2. Then use the ImageIndex property for TreeViewItem

This is how I do it and it works great.

Also in your example you use

 g.Draw(imageList.Images[0], ...) 

instead

 imageList.Draw(g, ...) 
+3
source

Source: https://habr.com/ru/post/914464/


All Articles