简体   繁体   English

ImageList上的32位图像

[英]32-bit images on ImageList

I've got the following image: 我有以下图像:

图标

I'm reading it from resources, putting into ImageList and then reading it from ImageList to draw on my control's surface. 我正在从资源中读取它,放入ImageList中,然后从ImageList中读取它以在控件的表面上绘制。 But when I'm doing that, image seems to loose information about alpha channel: 但是当我这样做时,图像似乎失去了有关Alpha通道的信息:

截图

Here are all relevant pieces of code: 这是所有相关的代码段:

Program.cs Program.cs中

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

MainForm.cs 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. 我实际上在( info.Icon )和( ilFilters.Images[0] )之后保存了图像,并将其放入图像列表。 The first version was correct, second - corrupted. 第一个版本正确,第二个-损坏。 Seems like ImageList damaged the image. 好像ImageList损坏了图像。

I also tried converting the PNG to 32-bit BMP image. 我还尝试将PNG转换为32位BMP图像。 No success. 没有成功

The system I'm running is Windows 7. .NET 4.5.1, Visual Studio 2013 Community. 我正在运行的系统是Windows7。.NET4.5.1,Visual Studio 2013社区。

How can I keep alpha channel of the image when adding to imagelist? 添加到图像列表时,如何保持图像的Alpha通道?


Edit: Proof of concept application . 编辑: 概念验证应用

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. 基本上,您必须在WinForms设计器中定义ImageList

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. Form的设计器视图中,从ToolBox创建一个ImageList
  2. In designer, set ColorDepth to "Depth32Bit", the correct image size and TransparentColor to "Transparent". 在设计器中,将ColorDepth设置为“ Depth32Bit”,正确的图像尺寸,将TransparentColor设置为“ Transparent”。
  3. After InitializeComponent() , add your images InitializeComponent() ,添加图像

Like this: 像这样:

ImageList1.Images.AddRange(new[]
{
    Resources.IconPlay,
    Resources.IconPause,
    Resources.IconStop,
    [...]
});
  1. Assign the ImageList to the TreeView in the designer. 在设计器中将ImageList分配给TreeView。
  2. Then, use the ImageIndex property of the TreeViewItem 然后,使用TreeViewItem的ImageIndex属性

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, ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM