保存c#中pictureBox中的图像

本文关键字:图像 pictureBox 保存 | 更新日期: 2023-09-27 18:30:00

我的程序允许用户在一个图片框中绘制,这有点类似于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);
    }

保存c#中pictureBox中的图像

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

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

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

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

编辑:

根据下面的评论,这个问题很可能是因为PictureBox中没有加载任何图像。

看看这个。

我想你的图片箱没有照片。当您单击按钮1时,您的图片框图像为空。在运行代码之前,右键单击图片框并在属性中导入一些照片。

正如King King在上面演示的那样,重新创建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);   
}