我真的需要帮助,因为我根本无法弄清楚这一点。当它使用资源图像保存到字节arrat时它工作正常但是当有用户定义的图像时它应该使用那个并将它保存到数组并且程序崩溃并且我找不到问题。
编码:
//Check for image and if true save it to byte array
if (pictureBox1.Image != null)
{
using (MemoryStream ms = new MemoryStream())
{
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
arr = ms.ToArray();
}
}
else
{
using (MemoryStream ms = new MemoryStream())
{
AnimalMotel.Properties.Resources.nophotos.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
arr = ms.ToArray();
}
}
此处的 ELSE 部分完美运行,当有用户定义的图像时出现问题,然后它崩溃并给出:
An unhandled exception of type
'System.Runtime.InteropServices.ExternalException'
occurred in System.Drawing.dll
Additional information: A generic error occurred in GDI+.
上一个代码:
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "(*.Bmp)|*.Bmp|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
pictureBox1.Image = new Bitmap(myStream);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}