我想要完成的是动态生成图像并将其作为图像添加到 ItemMenuStrip。当我将其设置为背景图像时它可以工作,但当我尝试将其设置为图像时它会失败。我在这里做错了什么?
代码:
foreach (ColorData colordata in colors)
{
ToolStripMenuItem item = new ToolStripMenuItem
{
Image = GenerateColorImage(colordata.UserSelectedColor),
Text = colordata.ColorDescription,
ToolTipText = "Click to change color",
Tag = num
};
item.Click += (sender, e) => { setScreenColors_Click(sender, e, colors); };
list.Add(item);
num++;
// item.Dispose();
}
private Bitmap GenerateColorImage(Color color)
{
Bitmap colorimage = new Bitmap(16, 16);
using (Graphics graphics = Graphics.FromImage(colorimage))
{
graphics.Clear(color);
}
return colorimage;
}