32-bit images on ImageList
Asked Answered
L

1

4

I've got the following image:

Icon

I'm reading it from resources, putting into ImageList and then reading it from ImageList to draw on my control's surface. But when I'm doing that, image seems to loose information about alpha channel:

Screenshot

Here are all relevant pieces of code:

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]) putting it into the image list. The first version was correct, second - corrupted. Seems like ImageList damaged the image.

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

The system I'm running is Windows 7. .NET 4.5.1, Visual Studio 2013 Community.

How can I keep alpha channel of the image when adding to imagelist?


Edit: Proof of concept application.

Luce answered 6/3, 2015 at 15:30 Comment(7)
Is info.Icon of type Bitmap and how do you load it?Paralytic
@Paralytic It is. It's stored in the resources of another assembly. I've just tried to save it immediately before adding to imagelist and it still is valid (alpha channel)Luce
Try defining ilFilters.TransparentColor = System.Drawing.Color.Transparent if that works. It's a shot in the dark, but that's what the WinForms designer does when you create an ImageList in the designer.Paralytic
@Paralytic Tried. Still nothing...Luce
@Paralytic I've created a proof-of-concept application.Luce
Good. Send it to me.Paralytic
@Paralytic I added a link in the question.Luce
P
8

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

In order to provide a complete answer, here are all the steps I've taken to make it work:

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

Like this:

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

This is how I do it and it works great.

Also, in your example, you use

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

instead of

imageList.Draw(g, ...)
Paralytic answered 6/3, 2015 at 16:16 Comment(3)
Very close, but a second ago I've just found even more precise cause: I'm using g.Draw(imageList.Images[0], ...) instead of imageList.Draw(g, ...). When I use the latter, everything works fine. You may add it to your answer and I'll accept it :)Luce
Usualy, coding is 80% bug fixing and 20% coding. Not to mention that these are the 20% left after planning ;)Paralytic
setting the colour depth to 32 bit worked for me :)Severus

© 2022 - 2024 — McMap. All rights reserved.