My program allows the user to draw in a picture box that is sort of a similar way to MS paint, and right now I'm trying to save the pictureBox as a .jpg file but I'm having null exception error when trying to do so.
Edit: should mention that this is a NullReferenceException
Here is my save button where I get the exception error:
private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save(@"C:\New folder\picture.jpg", ImageFormat.Jpeg);
}
and here is the rest of my code:
public Form2()
{
InitializeComponent();
//creates items for combobox brush sizes
for (int i = 1; i <= 20; i++)
{
string[] numbers = { i.ToString() };
comboBox1.Items.AddRange(numbers);
}
}
bool paint = false;
SolidBrush color = new SolidBrush(Color.Black);
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
paint = true;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
paint = false;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (paint == true)
{
int brushSize = Convert.ToInt32(comboBox1.SelectedItem);
Graphics g = pictureBox1.CreateGraphics();
if (comboBox1.SelectedIndex > 0)
{
g.FillEllipse(color, e.X, e.Y, brushSize, brushSize);
}
else
{
g.FillEllipse(color, e.X, e.Y, 10, 10);
}
g.Dispose();
}
}
//button that opens colour dialog box
private void button1_Click_1(object sender, EventArgs e)
{
ColorDialog cld = new ColorDialog();
if (cld.ShowDialog() == DialogResult.OK)
{
color = new SolidBrush(cld.Color);
}
}
//Button that clears pictureBox
private void Button2_Click_1(object sender, EventArgs e)
{
Graphics g1 = pictureBox1.CreateGraphics();
g1.Clear(pictureBox1.BackColor);
}