2
我正在使用C#窗体。当试图将图像保存到MemoryStream中时发生GDI +异常的一般错误
我的代码:
private void Openbutton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
SurveyDiagrampictureBox.Image = Bitmap.FromFile(openFileDialog.FileName);
MemoryStream memoryStream = new MemoryStream();
SurveyDiagrampictureBox.Image.Save(memoryStream, ImageFormat.Jpeg);
SurveyDiagram = memoryStream.GetBuffer();
}
}
它并不总是发生,异常踩着这条线时抛出:SurveyDiagrampictureBox.Image.Save(memoryStream, ImageFormat.Jpeg);
异常消息:
类型的未处理的异常'System.Runtime.InteropServices.ExternalException'发生在 System.Drawing.dll
附加信息:GDI +中发生了一般性错误。
好的,有用的错误信息FTW。 – Phil
在黑暗中拍摄:尝试初始化MemoryStream,其容量值稍大于jpeg文件的预期大小。 (宽度*高度可能是一个体面的估计)。 – selbie
@JatSing这是一个常见的错误,在Google上有40,000页。如果你希望我们帮忙'提供一个repro - 包括图像'。在过去,我在一个非常大的网站上发现了这个错误,当我们追踪它时,图像实际上是腐败的,但是还有其他很多原因。 –