如何通过按下WinForms中的按钮来绘制图像?

时间:2010-11-22 14:05:21

标签: c# winforms image graphics drawing

我必须做这个编辑器(BPMN编辑器)而且我被卡住了。我在表单中有一个图像,上面有一个图像,我想要这个:当我点击按钮然后我按下我的画布点击(绘制区域)将按钮放在那里。

1 个答案:

答案 0 :(得分:3)

public class Shape
{
    public float X { get; set; }
    public float Y { get; set; }
    public Image Image { get; set; }
}

和代码:

    private string _currentTool;
    private readonly List<Shape> _shapes;

    private void Button1Click(object sender, EventArgs e)
    {
        _currentTool = "img";
    }

    private void PictureBox1MouseDown(object sender, MouseEventArgs e)
    {
        switch (_currentTool)
        {
            case "img":
                _shapes.Add(new Shape {Image = button1.Image, X = e.X, Y = e.Y});
                pictureBox1.Invalidate();
                break;
        }
    }

    private void PictureBox1Paint(object sender, PaintEventArgs e)
    {
        foreach (var shape in _shapes)
        {
            e.Graphics.DrawImage(shape.Image, shape.X, shape.Y);
        }
    }