【发布时间】:2011-05-13 20:53:22
【问题描述】:
我必须做这个编辑器(BPMN 编辑器),但我被卡住了。我在表单中有这个按钮,上面有一个图像,我想要这个:当我点击按钮然后点击我的画布(绘制区域)将按钮中的图像放在那里。
【问题讨论】:
标签: c# winforms image graphics drawing
我必须做这个编辑器(BPMN 编辑器),但我被卡住了。我在表单中有这个按钮,上面有一个图像,我想要这个:当我点击按钮然后点击我的画布(绘制区域)将按钮中的图像放在那里。
【问题讨论】:
标签: c# winforms image graphics drawing
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 对象上绘制什么,它返回的内容都不会是持久的;也就是说,只要重新绘制窗口,它就会消失。您需要处理PictureBox 的Paint 事件并绘制到它提供的e.Graphics 对象上。
using。
CreateGraphics,并且编辑更正了这一点。不再需要处理任何东西。