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.