在c#中从pictureBox保存图像

时间:2013-09-01 21:45:55

标签: c# save picturebox nullreferenceexception

我的程序允许用户绘制一个类似于MS绘图的图片框,现在我正在尝试将pictureBox保存为.jpg文件,但是我遇到了null异常错误试图这样做。

编辑:应该提到这是一个NullReferenceException

这是我的保存按钮,我收到异常错误:

   private void button3_Click(object sender, EventArgs e)
    {
        pictureBox1.Image.Save(@"C:\New folder\picture.jpg", ImageFormat.Jpeg);
    }

这是我的其余代码:

    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);
    }

4 个答案:

答案 0 :(得分:1)

您应该通过相应的Graphics对象在图像上绘制所有内容。以下是我为您纠正的罚款代码,它至少比您的代码更好,更简洁:

 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);
}

答案 1 :(得分:0)

如果错误为ArgumentNullException,请确保您要保存的文件夹存在

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

编辑:

除了下面的评论之外,这个问题很可能是因为您没有在PictureBox中加载图像而引起的。

请参阅this

答案 2 :(得分:0)

我认为你的图片框没有照片。单击button1时,图片框图像为空。在运行代码之前,单击您的图片框并在属性中导入一些照片。

答案 3 :(得分:0)

King King演示above时,重新创建Graphics对象可能会导致闪烁。此外,您必须使用FromImage创建位图,否则您的绘图将不会出现在图像中,并且无论您在屏幕上看到什么,它都将保持为空。

//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);   

}