title: Draw on pictureBox with mouse
body: <p>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.</p>
  
  <p>What program is meant to do:
  Open image, allow user to draw on it, and allow user to save modified image</p>
  
  <p>Old code:</p>
  
  <pre><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;
  }
  </code></pre>
  
  <p>I am trying to use <code>picBox_Paint(PaintEventArgs...)</code> 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.</p>
  
  <p>Or if there is a way to make DrawLine on mouseEvent Permanent.</p>
  
  <p>Current code:</p>
  
  <pre><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);
  }
  </code></pre>
  
  <p>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.</p>