c# - ImageList 上的 32 位图像

标签 c# image winforms alpha-transparency imagelist

我有下面的图片:

Icon

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

Screenshot

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

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 Community。

添加到图像列表时如何保留图像的 alpha channel ?


编辑: Proof of concept application .

最佳答案

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

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

  1. Form 的设计器 View 中,从 ToolBox 创建一个 ImageList
  2. 在设计器中,将 ColorDepth 设置为“Depth32Bit”、正确的图像大小并将 TransparentColor 设置为“透明”。
  3. InitializeComponent() 之后,添加您的图片

像这样:

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

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

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

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

代替

imageList.Draw(g, ...)

关于c# - ImageList 上的 32 位图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28902091/

相关文章:

c# - 如何为 DataGridViewComboBox 列设置值?

c# - 验证驾驶执照号码?

html - HTML 中的图像和文本标记,其中图像调整大小以适合容器

C# Winform( Entity Framework )- 将数据绑定(bind) DataGridView 或 BindingSource 转换为 DataTable

c# - 结合 C# 和 C 的优点/缺点

c# - 从工具箱中的程序集隐藏所有 WPF 用户控件

c# - 将两个复选框绑定(bind)到 MVVM 中的可为 null 的 bool 属性

html - 当前页面上的按钮图像

c# - 如何在框架中拆分 .gif?

c# - 从另一个表单获取文本框值