ImageList上的32位图像
我有以下图片:
我正在从资源中读取它,放入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通道?
编辑: 概念validation应用程序 。
从您发布的概念certificate中,我找到了一种简单的方法来使其工作。 基本上,您必须在WinForms设计器中定义ImageList
。
为了提供完整的答案,以下是我为使其工作而采取的所有步骤:
- 在
Form
的设计器视图中,从ToolBox创建一个ImageList
。 - 在设计器中,将ColorDepth设置为“Depth32Bit”,将正确的图像大小和TransparentColor设置为“Transparent”。
- 在
InitializeComponent()
,添加图像
像这样:
ImageList1.Images.AddRange(new[] { Resources.IconPlay, Resources.IconPause, Resources.IconStop, [...] });
- 将ImageList分配给设计器中的TreeView。
- 然后,使用TreeViewItem的ImageIndex属性
这就是我这样做的方式,效果很好。
此外,在您的示例中,您使用
g.Draw(imageList.Images[0], ...)
代替
imageList.Draw(g, ...)