0

我想要完成的是动态生成图像并将其作为图像添加到 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;
    }
4

1 回答 1

0

编辑:这可能是你正在寻找的,

private Bitmap GenerateColorImage(Color color)
        {
            Bitmap colorimage = new Bitmap(16, 16) ;
            using (Graphics graphics = Graphics.FromImage(colorimage))
            {
                graphics.Clear(color);
                using (SolidBrush brush = new SolidBrush(colorimage)
                {
                   graphics.FillRectangle(brush, 0, 0, 16, 16);
                }

            }
            return colorimage;
        }
于 2013-10-09T07:35:58.393 回答