ImageList 上的 32 位图像

32-bit images on ImageList

我得到了以下图片:

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

以下是所有相关代码片段:

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;
    }
}

其实我把图片存入图片列表之前(info.Icon)和之后(ilFilters.Images[0])。第一个版本是正确的,第二个版本已损坏。似乎 ImageList 损坏了图像。

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

我 运行 的系统是 Windows 7. .NET 4.5.1,Visual Studio 2013 社区。

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


编辑: Proof of concept application.

根据您发布的概念证明,我找到了一种使其工作的简单方法。基本上,您必须在 WinForms 设计器中定义 ImageList

为了提供完整的答案,以下是我为使其有效而采取的所有步骤:

  1. Form 的设计器视图中,从工具箱创建一个 ImageList
  2. 在设计器中,将 ColorDepth 设置为 "Depth32Bit",正确的图像大小,将 TransparentColor 设置为 "Transparent"。
  3. InitializeComponent() 之后,添加您的图片

像这样:

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

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

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

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

而不是

imageList.Draw(g, ...)