I recently started programming in C# obviously and was trying to do a simple WinForms app that takes mouse coordinates and scales a Rectangle accor
When drawing on a Control's surface, you always use the Paint event of that Control.
Do not ever try to store it's Graphics object (it isn't valid anymore as soon as the Control is invalidated (repainted)).
Don't try to use its PaintEventArgs in some exotic way. Use the Graphics object it references inside the Paint event only.
When a more complex procedure is required to draw more, different, shapes, you can pass the e.Graphics object provided by the Paint event to different methods, which will use the e.Graphics object to perform specialized drawings.
In the example, I'm storing each drawn Rectangle's coordinates in a specialized class (DrawingRectangle, a simplified structure here) and I use a List to access these references.
When the MouseDown event reports that the Left button is pressed on the Control's surface, the e.Location is stored as the DrawingRectangle.Location (this value can change, depending on the mouse pointer direction) and the DrawingRectangle.StartPoint (a reference measure that doesn't change).
When the Mouse is moved, the current e.Location coordinates values are subtracted from the stored Rectangle starting point coordinates (plus some calculations that allow to draw the Rectangle from all sides). This measure is the Size of the Rectangle.
Each time a Left MouseDown is detected, a new class element is added to the List.
To remove a Rectangle from the drawing, you just need to remove its reference from the List and Invalidate() the Control that provides the drawing surface.
To clear the drawing surface, clear the List (drawingRects.Clear()) and call Invalidate().
Some other examples here:
Transparent Overlapping Circular Progress Bars
GraphicsPath and Matrix classes
Connecting different shapes
Drawing Transparent/Translucent Custom Controls
List drawingRects = new List();
public class DrawingRectangle
{
public Point Location { get; set; }
public Size Size { get; set; }
public Point StartPosition { get; set; }
public Color DrawingcColor { get; set; } = Color.LightGreen;
public float PenSize { get; set; } = 3f;
}
private void form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
drawingRects.Add(new DrawingRectangle() {
Location = e.Location, Size = Size.Empty, StartPosition = e.Location
});
}
}
private void form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DrawingRectangle rect = drawingRects.Last();
if (e.Y < rect.StartPosition.Y) { rect.Location = new Point(rect.Location.Y, e.Y); }
if (e.X < rect.StartPosition.X) { rect.Location = new Point(e.X, rect.Location.Y); }
rect.Size = new Size(Math.Abs(rect.StartPosition.X - e.X), Math.Abs(rect.StartPosition.Y - e.Y));
drawingRects[drawingRects.Count - 1] = rect;
this.Invalidate();
}
}
private void form1_Paint(object sender, PaintEventArgs e)
{
if (drawingRects.Count == 0) return;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
foreach (var rect in drawingRects) {
using (Pen pen = new Pen(rect.DrawingcColor, rect.PenSize)) {
e.Graphics.DrawRectangle(pen, new Rectangle(rect.Location, rect.Size));
};
}
}
private void btnClear_Click(object sender, EventArgs e)
{
drawingRects.Clear();
this.Invalidate();
}