将图形对象拖动到位图之上

发布于 2024-10-16 01:11:09 字数 738 浏览 2 评论 0原文

我试图在鼠标移动事件中拖动图片框周围的形状,但很难让它顺利移动。图片框有一个图像作为背景加载,我希望图形对象在单击并拖动鼠标时在图像顶部拖动一个圆圈。

我通过每次鼠标移动时创建原始图像的克隆并重新加载图片框来工作,但似乎它们必须是实现此目的的更好方法。

每次添加的任何图形都保留在图像上,无需重新加载原始位图,从而创建更像绘画应用程序的轨迹。

如何清除以前的绘图而不每次都重新加载整个图像?任何帮助表示赞赏。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情栀口红 2024-10-23 01:11:09

检查这个示例,它更简单

//Load Image
Bitmap TestImage = new Bitmap(FileName);
//Create Graphics Object
Graphics g = Graphics.FromImage(TestImage);                   
g.DrawEllipse(new Pen(Color.Red), i, j,0.5F, 0.5F);
//View Your Results
pictureBox1.Image = TestImage;

check this sample it is simpler

//Load Image
Bitmap TestImage = new Bitmap(FileName);
//Create Graphics Object
Graphics g = Graphics.FromImage(TestImage);                   
g.DrawEllipse(new Pen(Color.Red), i, j,0.5F, 0.5F);
//View Your Results
pictureBox1.Image = TestImage;
﹂绝世的画 2024-10-23 01:11:09

要以最佳方式解决问题,请使用 picCanvas .Paint 事件。
在 mousemove 事件中设置位置,并使用该位置在 Paint 事件中进行绘制。

    Point pos = Point.Empty;// or your initial position

    private void picCanvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (_drag)
        {
            pos = e.Location;
        }
    }
    private void picCanvas_Paint(object sender, PaintEventArgs e)
    {
        if (_drag)
        {
            Graphics g = e.Graphics;//The event handler sends us the graphics object to use for painting
            g.DrawEllipse(_whitePen, pos.X, pos.Y, 10, 10); 
        }
    }

您应该将 Paint 事件添加到控件并在表单加载或某些初始化函数中设置图像。

picCanvas.Image = (Bitmap)_original.Clone(); 

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.

    Point pos = Point.Empty;// or your initial position

    private void picCanvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (_drag)
        {
            pos = e.Location;
        }
    }
    private void picCanvas_Paint(object sender, PaintEventArgs e)
    {
        if (_drag)
        {
            Graphics g = e.Graphics;//The event handler sends us the graphics object to use for painting
            g.DrawEllipse(_whitePen, pos.X, pos.Y, 10, 10); 
        }
    }

You should add the Paint event to the Control and set the image at formload or some initialization function.

picCanvas.Image = (Bitmap)_original.Clone(); 
卸妝后依然美 2024-10-23 01:11:09

使用霍尼比斯的上述答案,我最终得到了这个。

加载图像并使图片无效以导致刷新,

picCanvas.Image = image;
picCanvas.Invalidate()

然后在绘制事件中

private void picCanvas_Paint(object sender, PaintEventArgs e)
{
  if (_drag)         
  {
    using (Pen pen = new Pen(Color.White, 2))
    {
      e.Graphics.DrawEllipse(pen, pos.X, pos.Y, 10, 10);
    }
  }
}

Using the above answer from Honibis I ended up with this.

load in the image and invalidate the picture to cause a refresh

picCanvas.Image = image;
picCanvas.Invalidate()

then in the paint event

private void picCanvas_Paint(object sender, PaintEventArgs e)
{
  if (_drag)         
  {
    using (Pen pen = new Pen(Color.White, 2))
    {
      e.Graphics.DrawEllipse(pen, pos.X, pos.Y, 10, 10);
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文