0

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);
    }
Rin
  • 1
  • 1
  • 2
  • 4
  • Are you sure the `Button_Click_1` doesn't occur before you are trying to save the image ? – devavx Sep 01 '13 at 21:53
  • @Aviral How does this first button click effect that save button click? – Rin Sep 01 '13 at 22:05
  • You never actually assigned the PictureBox.Image property, opting to paint directly instead. Which works well. That property is still *null* though, kaboom when you try to save it. – Hans Passant Sep 01 '13 at 22:25

4 Answers4

1

You should draw everything on an Image via the corresponding Graphics object. Here is the fined code I corrected for you, it's at least better and succinct than your 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);
    }
    //initialize a blank image for your PictureBox
    pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    g = Graphics.FromImage(pictureBox1.Image);
 }
 Graphics g;
SolidBrush color = new SolidBrush(Color.Black);

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Left) {
        int brushSize = comboBox1.SelectedIndex > 0 ?
                        Convert.ToInt32(comboBox1.SelectedItem) : 10;
        g.FillEllipse(color, e.X, e.Y, brushSize, brushSize);
        pictureBox1.Invalidate();//This is important to re-draw the updated Image
    }
}
//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) {
    g.Clear(pictureBox1.BackColor);
}
private void button3_Click(object sender, EventArgs e) {
    pictureBox1.Image.Save(@"C:\New folder\picture.jpg", ImageFormat.Jpeg);
}
King King
  • 61,710
  • 16
  • 105
  • 130
  • Hello, I greatly appreciate it that you spent time fixing my code :) it seems to nearly work perfectly. It does save the drawing, but it has a black coloured background for some reason. – Rin Sep 01 '13 at 23:45
0

If the error is ArgumentNullException then make sure that the folder you are trying to save to exists

http://msdn.microsoft.com/en-us/library/9t4syfhh.aspx

EDIT:

Further to the comment below, this problem is most likely caused because you have no image loaded in the PictureBox.

See this.

Community
  • 1
  • 1
iabbott
  • 873
  • 1
  • 8
  • 23
0

I think your picturebox has no photo. When you click button1 your picture box image is null. Rigth click on your picture box and in properties import some photos before run code.

  • Hello, I added a white background under properties which got rid of the null error, but it saves a blank white image even when I draw on it. Do you perhaps know any good sources or useful codes I could use for this problem? – Rin Sep 01 '13 at 22:43
  • @meau94 Hello, I think this link help your problem [link](http://kishordgupta.wordpress.com/2011/02/18/c-tips-how-to-draw-on-a-picturebox-image-using-mouse-by-c/) – errorintheapplication Sep 01 '13 at 22:48
0

As King King demonstrated above, re-creating the Graphics object may cause flicker. Also, you have to create the bitmap using FromImage or your drawing will not be to the image, and it will remain null despite what you see on screen.

//declare graphics globally
Graphics g; 

private void Form_Load(object sender, EventArgs e)
{

    picCanvas.Image = new Bitmap(picCanvas.Width, picCanvas.Height);

    // create the graphics object here and not in DrawLine, which 
    // may cause flicker each time its instantiated

    graphics = Graphics.FromImage(picCanvas.Image);

    DrawLine();

}

private void DrawLine()
{

    //Do not recreate the Graphics object here. 
    //Recreating it seems to 'erase' the existing image
    //Which causes flicker that double-buffering can't manage

    System.Drawing.Pen pen;
    pen.Color = Color.Black;

    // If you create the graphics object from the bitmap, this
    // should paint to the bitmap, so the Image object won't be null
    g.DrawLine(1, 1, picCanvas.Width - 2, picCanvas.Height - 2);   

}
Community
  • 1
  • 1
Victor Stoddard
  • 3,582
  • 2
  • 27
  • 27