我已编写此代码,但它会抛出一般错误
“对象引用未设置为对象的实例。”
我相信这是因为我试图在没有实际设置图像的情况下保存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");
答案 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
。