Skip to content

Instantly share code, notes, and snippets.

@karimo94
Created March 1, 2014 00:40
Show Gist options
  • Save karimo94/9282894 to your computer and use it in GitHub Desktop.
Save karimo94/9282894 to your computer and use it in GitHub Desktop.
public partial class Form1 : Form
{
Point lastPoint = Point.Empty;//Point.Empty represents null for a Point object
bool isMouseDown = new Boolean();
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
lastPoint = e.Location;//we assign the lastPoint to the current mouse position
isMouseDown = true;//we set to true because our mouse button is down (clicked)
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown == true)//check to see if the mouse button is down
{
if (lastPoint != null)//if our last point is not null, which in this case we have assigned above
{
if (pictureBox1.Image == null)
{
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = bmp;
}
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{//we need to create a Graphics object to draw on the picture box, its our main tool
//when making a Pen object, you can just give it color only or give it color and pen size
g.DrawLine(new Pen(Color.Black, 2), lastPoint, e.Location);
g.SmoothingMode = SmoothingMode.AntiAliasing;
//this is to give the drawing a more smoother, less sharper look
}
pictureBox1.Invalidate();//refreshes the picturebox
lastPoint = e.Location;//keep assigning the lastPoint to the current mouse position
}
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
lastPoint = Point.Empty;
//set the previous point back to null if the user lets go of the mouse button
}
private void clearButton_Click(object sender, EventArgs e)//our clearing button
{
if (pictureBox1.Image != null)
{
pictureBox1.Image = null;
Invalidate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment