0

我的程序允许用户在类似于 MS 绘画的方式中绘制图片框,现在我正在尝试将图片框保存为 .jpg 文件,但尝试执行时出现空异常错误所以。

编辑:应该提到这是一个 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

4 回答 4

1

您应该通过相应的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);
}
于 2013-09-01T23:07:33.867 回答
0

如果错误是ArgumentNullException,请确保您尝试保存的文件夹存在

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

编辑:

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

看到这个

于 2013-09-01T21:57:24.117 回答
0

我认为您的图片框没有照片。当您单击 button1 时,您的图片框图像为空。右键单击您的图片框,然后在属性中导入一些照片,然后再运行代码。

于 2013-09-01T22:06:40.407 回答
0

正如上面King King演示的那样,重新创建对象可能会导致闪烁。此外,您必须使用创建位图,否则您的绘图将不是图像,并且无论您在屏幕上看到什么,它都将保持为空。GraphicsFromImage

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

}
于 2014-08-19T23:26:31.620 回答