1

I am trying to make a paint program in C# I started out using a panel, and using a pen, but whenever the window refreshed, the drawing was removed.

What program is meant to do: Open image, allow user to draw on it, and allow user to save modified image

Old code:

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;
}

I am trying to use picBox_Paint(PaintEventArgs...) to draw, but I can't figure out how to capture _MouseDown/_MouseMove/_MouseUp with the paint event to draw when the mouse is moved over the pictureBox and MouseDown is true.

Or if there is a way to make DrawLine on mouseEvent Permanent.

Current code:

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);
}

This works, and does't get removed on window refresh, but I don't know how to get mouse input now, and use MouseEvent to get the mouse points for drawing.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
smasher248
  • 23
  • 6
  • 1
    For your requirement you don't even need `PictureBox` (it's good to display bitmap, but it require refresh if you change one and that's pretty equal to just painting bitmap yourself without `PictureBox`). Open/create new bitmap and simply [draw it](http://stackoverflow.com/q/12056093/1997232) on some `Panel` (you can make custom Panel control to wrap all this functionality). Handle mouse events as you do and paint [into bitmap](http://stackoverflow.com/q/6311545/1997232) and then simply invalidate panel (so it will redraw itself, displaying last changes). Then bitmap persist and can be saved. – Sinatr Mar 28 '17 at 09:50
  • One more thing to be aware of is that you are creating a lot of new GDI objects without disposing of them. Lines like `SolidBrush sb = new SolidBrush(btn_brushColor.BackColor);` will actually create a brand new brush with a GDI handle every time the mouse moves. If you don't dispose of those brushes (or better yet, create the brush once and reuse it), you will eventually have serious performance issues or errors. – Bradley Uffner Mar 28 '17 at 18:21
  • Where's that g variable coming from in the MouseMove event? Never store a graphic object. – LarsTech Mar 28 '17 at 18:25

0 Answers0