加载图像,通过点击按钮在其上绘制形状,用鼠标在其上绘制线条

本文关键字:绘制 鼠标 图像 按钮 加载 | 更新日期: 2023-09-27 18:17:11

我想加载一个图像(例如在面板中),并通过使用我所拥有的函数来测量picture__的边缘,并通过单击按钮在图像上绘制边缘的线条。之后,我想用鼠标在同一图像上绘制额外的线条。我还希望能够通过鼠标擦除绘制的线条,而不会稍后擦除图像。我不知道在每种情况下应该用哪个函数。一种方法可能是,我用我的图像设置面板的backgrounimage,并使用paint函数绘制额外的线条(用鼠标绘制)。如果我使用这个方法,那么我应该使用哪个函数来绘制edges_drawn的线通过点击按钮?有没有更好的办法?

加载图像,通过点击按钮在其上绘制形状,用鼠标在其上绘制线条

见下面的代码。希望这对你有帮助

Point startPoint = new Point();
bool dragging = false;
int testOne = 30;
int testTwo = 30;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (dragging)
    {
        int diffX = (pictureBox1.PointToClient(e.Location).X - startPoint.X);
        int diffY = (pictureBox1.PointToClient(e.Location).Y - startPoint.Y);
        label9.Text = diffX.ToString();   //Works, shows desired result
        label10.Text = diffY.ToString();  //also works fine
        testOne = (testOne + diffX); //Issue here
        testTwo = (testTwo + diffY); //and here
        label11.Text = (testOne).ToString(); //Unexpected results output
        label12.Text = (testTwo).ToString(); 
    }
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if (!dragging) //Incase the mouse down was repeating, it's not
    {
        startPoint = pictureBox1.PointToClient(e.Location);
        dragging = true;
    }
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    if (dragging)
        dragging = false;
}