【问题标题】:How to draw an image by pressing a button in WinForms?如何通过按下 WinForms 中的按钮来绘制图像?
【发布时间】:2011-05-13 20:53:22
【问题描述】:

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

【问题讨论】:

    标签: c# winforms image graphics drawing


    【解决方案1】:
    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);
            }
        }
    

    【讨论】:

    • 您永远不应该使用CreateGraphics 绘图。无论您在 Graphics 对象上绘制什么,它返回的内容都不会是持久的;也就是说,只要重新绘制窗口,它就会消失。您需要处理PictureBoxPaint 事件并绘制到它提供的e.Graphics 对象上。
    • Navid,你没有处理图形对象。你应该在那里使用using
    • @Charlie:正如我所指出的,他不应该使用CreateGraphics,并且编辑更正了这一点。不再需要处理任何东西。
    • 谢谢。如果我有任何其他问题,我会问你,因为我认为这是一个复杂的项目,而且我对编程不太擅长。
    • 你好。现在我必须这样做:在画布上的两个图像之间绘制一条弧线,所以当我单击带有弧线的按钮并单击一个图像以将弧线粘贴在其上然后绘制弧线时根据我的需要并将其粘贴到第二张图片中。
    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 2019-12-03
    相关资源
    最近更新 更多