如何从较小的组件图像构建C#ImageList图像?

时间:2020-03-06 14:23:28  来源:igfitidea点击:

我想为CWinForms TreeList控件制作状态图标。状态是其他状态的组合(例如,用户节点可能处于非活动状态或者被禁止,或者处于非活动状态并被禁止),状态图标由不重叠的较小字形组成。

如果可以避免的话,我真的很想避免必须手动生成状态图标的所有可能排列。

是否可以创建可用于以编程方式生成ImageList的图像列表(或者仅一堆位图资源等)?

我在四处逛逛System.Drawing类,没有什么让我兴奋。另外,我还停留在.Net 2.0上。

解决方案

Bitmap image1 = ...
Bitmap image2 = ...

Bitmap combined = new Bitmap(image1.Width, image1.Height);
using (Graphics g = Graphics.FromImage(combined)) {
  g.DrawImage(image1, new Point(0, 0));
  g.DrawImage(image2, new Point(0, 0);
}

imageList.Add(combined);

只需使用ImageList中的Images.Add添加单个图像即可。因此,类似:

Image img = Image.FromStream( /*get stream from resources*/ );
ImageList1.Images.Add( img );