ImageList上的32位图像

Spo*_*ook 3 c# image imagelist alpha-transparency winforms

我有以下图像:

图标

我正在从资源中读取它,放入ImageList中,然后从ImageList中读取它以在控件的表面上绘制。但是当我这样做时,图像似乎失去了有关Alpha通道的信息:

屏幕截图

这是所有相关的代码段:

Program.cs

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

实际上,我在(info.Icon)之前和之后(ilFilters.Images[0])将图像保存到图像列表中。第一个版本正确,第二个-损坏。好像ImageList损坏了图像。

我还尝试将PNG转换为32位BMP图像。没有成功

我正在运行的系统是Windows7。.NET4.5.1,Visual Studio 2013社区。

添加到图像列表时,如何保持图像的Alpha通道?


编辑: 概念验证应用

byt*_*e77 5

从您发布的概念证明中,我找到了使之生效的简单方法。基本上,您必须ImageList在WinForms设计器中定义。

为了提供完整的答案,请按照以下所有步骤进行操作:

  1. 在的设计器视图中FormImageList从ToolBox中创建一个。
  2. 在设计器中,将ColorDepth设置为“ Depth32Bit”,正确的图像尺寸,将TransparentColor设置为“ Transparent”。
  3. 之后InitializeComponent(),添加您的图片

像这样:

ImageList1.Images.AddRange(new[]
{
    Resources.IconPlay,
    Resources.IconPause,
    Resources.IconStop,
    [...]
});
Run Code Online (Sandbox Code Playgroud)
  1. 在设计器中将ImageList分配给TreeView。
  2. 然后,使用TreeViewItem的ImageIndex属性

这就是我的做法,效果很好。

另外,在您的示例中,您使用

g.Draw(imageList.Images[0], ...)
Run Code Online (Sandbox Code Playgroud)

代替

imageList.Draw(g, ...)
Run Code Online (Sandbox Code Playgroud)