【问题标题】:32-bit images on ImageListImageList 上的 32 位图像
【发布时间】:2015-03-07 00:21:49
【问题描述】:

我有以下图片:

我从资源中读取它,放入 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 Community。

添加到imagelist时如何保持图像的alpha通道?


编辑: Proof of concept application.

【问题讨论】:

  • info.IconBitmap 类型的吗?如何加载它?
  • @bytecode77 是的。它存储在另一个程序集的资源中。我刚刚尝试在添加到 imagelist 之前立即保存它,它仍然有效(alpha 通道)
  • 尝试定义ilFilters.TransparentColor = System.Drawing.Color.Transparent,如果可行的话。这是在黑暗中拍摄的,但是当您在设计器中创建 ImageList 时,WinForms 设计器就是这样做的。
  • @bytecode77 试过了。还是什么都没有……
  • @bytecode77 我创建了一个概念验证应用程序。

标签: c# image winforms alpha-transparency imagelist


【解决方案1】:

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

为了提供完整的答案,以下是我为使其发挥作用所采取的所有步骤:

  1. Form 的设计器视图中,从工具箱中创建一个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, ...)

【讨论】:

  • 非常接近,但一秒钟前我刚刚找到了更准确的原因:我使用的是g.Draw(imageList.Images[0], ...) 而不是imageList.Draw(g, ...)。当我使用后者时,一切正常。您可以将其添加到您的答案中,我会接受它:)
  • 通常,编码是 80% 的错误修复和 20% 的编码。更不用说这些是 计划 之后剩下的 20% ;)
  • 将颜色深度设置为 32 位对我有用 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多