保存图片框图像

时间:2013-01-27 09:21:15

标签: c# save picturebox

我已编写此代码,但它会抛出一般错误

  

“对象引用未设置为对象的实例。”

我相信这是因为我试图在没有实际设置图像的情况下保存pictureBox.Image。如何保存pictureBox

中的图像
            // Left Arm Rear
            bmp = new Bitmap(4, 12);
            g3 = Graphics.FromImage(bmp);
            g3.DrawImageUnscaled(pictureBoxSkin.Image, -52, -20, bmp.Width, bmp.Height);
            g2 = pictureBoxLabel.CreateGraphics();
            bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);
            g2.DrawImageUnscaled(bmp, 28, 20, bmp.Width, bmp.Height);

            // Back
            bmp = new Bitmap(8, 12);
            g3 = Graphics.FromImage(bmp);
            g3.DrawImageUnscaled(pictureBoxSkin.Image, -32, -20, bmp.Width, bmp.Height);
            g2 = pictureBoxLabel.CreateGraphics();
            bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);
            g2.DrawImageUnscaled(bmp, 32, 20, bmp.Width, bmp.Height);

            pictureBoxLabel.Image.Save(openFileDialog1.InitialDirectory + Path.GetFileNameWithoutExtension(openFileDialog1.FileName) + "_label.Png");

1 个答案:

答案 0 :(得分:0)

如果NullReferanceException在最后一行投掷,则有一些可能性;

pictureBoxLabel.Image.Save(openFileDialog1.InitialDirectory + Path.GetFileNameWithoutExtension(openFileDialog1.FileName) + "_label.Png");
  • pictureBoxLabel可以是null
  • openFileDialog1可以是null

在调试时检查这些对象。

但如果pictureBoxLabel为空,则可能会在此行上抛出NullReferanceException;

g2 = pictureBoxLabel.CreateGraphics();

所以,我的钱是openFileDialog1 null