我有下面的图片:
我从资源中读取它,放入 ImageList,然后从 ImageList 读取它以在我的控件表面上绘制。但是当我这样做时,图像似乎丢失了有关 alpha channel 的信息:
以下是所有相关的代码片段:
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 ?
最佳答案
根据您发布的概念证明,我找到了一种使其工作的简单方法。基本上,您必须在 WinForms 设计器中定义 ImageList
。
为了提供完整的答案,以下是我为使其正常工作而采取的所有步骤:
- 在
Form
的设计器 View 中,从 ToolBox 创建一个ImageList
。 - 在设计器中,将 ColorDepth 设置为“Depth32Bit”、正确的图像大小并将 TransparentColor 设置为“透明”。
- 在
InitializeComponent()
之后,添加您的图片
像这样:
ImageList1.Images.AddRange(new[]
{
Resources.IconPlay,
Resources.IconPause,
Resources.IconStop,
[...]
});
- 在设计器中将 ImageList 分配给 TreeView。
- 然后,使用 TreeViewItem 的 ImageIndex 属性
我就是这样做的,效果很好。
此外,在您的示例中,您使用
g.Draw(imageList.Images[0], ...)
代替
imageList.Draw(g, ...)
关于c# - ImageList 上的 32 位图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28902091/