将图形对象拖动到位图之上
我试图在鼠标移动事件中拖动图片框周围的形状,但很难让它顺利移动。图片框有一个图像作为背景加载,我希望图形对象在单击并拖动鼠标时在图像顶部拖动一个圆圈。
我通过每次鼠标移动时创建原始图像的克隆并重新加载图片框来工作,但似乎它们必须是实现此目的的更好方法。
每次添加的任何图形都保留在图像上,无需重新加载原始位图,从而创建更像绘画应用程序的轨迹。
如何清除以前的绘图而不每次都重新加载整个图像?任何帮助表示赞赏。
private void picCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (_drag)
{
picCanvas.Image = (Bitmap)_original.Clone();
Graphics g = Graphics.FromImage((Bitmap)picCanvas.Image);
g.DrawEllipse(_whitePen, e.X, e.Y, 10, 10);
picCanvas.Invalidate();
}
}
private void picCanvas_MouseDown(object sender, MouseEventArgs e)
{
_drag = true;
}
private void picCanvas_MouseUp(object sender, MouseEventArgs e)
{
_drag = false;
}
I am attempting to drag a shape around a picturebox on the mousemove event but am struggling to get it to move smoothly. The picture box has an image loaded as the background and I would like the graphics object to drag a circle on top of the image when the mouse is clicked and dragged.
I have it working by creating a clone of the original image each time the mouse moves and reloading the picture box but it seems like their must be a better way to achieve this.
Without reloading the original bitmap each time any graphics added remain on the image creating a trail which is more like a paint application.
How do I clear previous drawings without reloading the entire image each time? Any help appreciated.
private void picCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (_drag)
{
picCanvas.Image = (Bitmap)_original.Clone();
Graphics g = Graphics.FromImage((Bitmap)picCanvas.Image);
g.DrawEllipse(_whitePen, e.X, e.Y, 10, 10);
picCanvas.Invalidate();
}
}
private void picCanvas_MouseDown(object sender, MouseEventArgs e)
{
_drag = true;
}
private void picCanvas_MouseUp(object sender, MouseEventArgs e)
{
_drag = false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查这个示例,它更简单
check this sample it is simpler
要以最佳方式解决问题,请使用 picCanvas .Paint 事件。
在 mousemove 事件中设置位置,并使用该位置在 Paint 事件中进行绘制。
您应该将 Paint 事件添加到控件并在表单加载或某些初始化函数中设置图像。
To solve the problem in the best way, use picCanvas.Paint event.
Set the positions at mousemove event and use that positions to draw at paint event.
You should add the Paint event to the Control and set the image at formload or some initialization function.
使用霍尼比斯的上述答案,我最终得到了这个。
加载图像并使图片无效以导致刷新,
然后在绘制事件中
Using the above answer from Honibis I ended up with this.
load in the image and invalidate the picture to cause a refresh
then in the paint event