使用鼠标

时间:2017-03-28 08:29:06

标签: c# graphics paint picturebox

我正在尝试用C#制作一个绘画程序我开始使用面板,并使用钢笔,但每当窗口刷新时,绘图就被移除了。

该计划的目的是什么: 打开图像,允许用户在其上绘图,并允许用户保存修改后的图像

旧代码:

private void canvas_MouseMove(object sender, MouseEventArgs e)
{
    if (canDraw)
    {
        //canvas.BackgroundImage = buffer;
        if (drawSquare)
        {
            isDrawn = true;
            SolidBrush sb = new SolidBrush(btn_brushColor.BackColor);
            Rectangle path1 = new Rectangle(e.X, e.Y, int.Parse(shapeSize.Text), int.Parse(shapeSize.Text));
            //Rectangle path1 = new Rectangle(100, 100, int.Parse(shapeSize.Text), int.Parse(shapeSize.Text));
            Rectangle path2 = new Rectangle(e.X + (int.Parse(shapeSize.Text) / 20), e.Y + (int.Parse(shapeSize.Text) / 20), (int.Parse(shapeSize.Text) / 10 * 9), (int.Parse(shapeSize.Text) / 10 * 9));
            //Rectangle path2 = new Rectangle(e.X, e.Y, 2/(int.Parse(shapeSize.Text)), 2/(int.Parse(shapeSize.Text)));

            // Create a region from the Outer circle.
            Region region = new Region(path1);

            // Exclude the Inner circle from the region
            region.Exclude(path2);

            // Draw the region to your Graphics object
            g.FillRegion(sb, region);
            //drawRectangle = false;
            debug.Text = ("recf");
        }

        if (drawEraser)
        {
            isDrawn = true;
            Pen d = new Pen(Color.White, float.Parse(cmb_brushSize.Text));
            g.DrawLine(d, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));
            initX = e.X;
            initY = e.Y;
        }

        if (drawbrush)
        {
            isDrawn = true;
            Pen p = new Pen(btn_brushColor.BackColor, float.Parse(cmb_brushSize.Text));
            g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));
            initX = e.X;
            initY = e.Y;
        }
    }
}

private void canvas_MouseDown(object sender, MouseEventArgs e)
{
    debug.Text = ("running");
    canDraw = true;
}

private void canvas_MouseUp(object sender, MouseEventArgs e)
{
    canDraw = false;
    initX = null;
    initY = null;
}

我正在尝试使用picBox_Paint(PaintEventArgs...)进行绘制,但是当鼠标移到pictureBox并且MouseDown为true时,我无法弄清楚如何使用绘制事件捕获_MouseDown / _MouseMove / _MouseUp。

或者,如果有办法在mouseEvent Permanent上创建DrawLine。

当前代码:

private void picBox_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.DrawString("This is a diagonal line drawn on the control",
        new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
    g.DrawLine(System.Drawing.Pens.Red, picBox.Left, picBox.Top,
        picBox.Right, picBox.Bottom);
}

这有效,并且在窗口刷新时不会被删除,但我现在不知道如何获取鼠标输入,并使用MouseEvent获取用于绘图的鼠标点。

0 个答案:

没有答案