C#动态生成图像并将其设置为ItemMenuStrip Image

时间:2013-10-09 07:23:29

标签: c# image dynamically-generated

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

1 个答案:

答案 0 :(得分: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;
        }