1

我已经编写了这段代码,但它会引发一般错误

“你调用的对象是空的。”

我相信这是因为我试图在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");
4

1 回答 1

0

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

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

在调试时检查这些对象。

但是如果pictureBoxLabel为null,也可以NullReferanceException在这条线上抛出;

g2 = pictureBoxLabel.CreateGraphics();

所以,我的钱是openFileDialog1为了null

于 2013-01-27T09:37:07.793 回答