[英]Getting OutOfMemoryException when I load a tga file of 1 mb in c#
我不知道问题到底出在哪里。 我可以加载小的jpeg文件,但是当我尝试加载tga文件时,出现异常。 我尝试调整图像的大小,但这也无济于事。
public static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
{
return (System.Drawing.Image)(new Bitmap(imgToResize, size));
}
private void imageToolStripMenuItem4_Click(object sender, EventArgs e)
{
if (tabControl1.TabCount == 0)
{
MessageBox.Show("Please add a form first");
return;
}
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "TGA (*.tga)|*.tga|JPEG (*.jpg)|*.jpg|BITMAP FILES (*.bmp)|*.bmp|PNG (*.png)|*.png";
openFileDialog1.FilterIndex = 1;
if (System.Windows.Forms.DialogResult.OK == openFileDialog1.ShowDialog())
{
BckImageRadioBtnGrp bimrbg=new BckImageRadioBtnGrp();
bimrbg.ShowDialog();
string result = bimrbg.getResult();
if (result != null)
{
switch (result)
{
case "Center" : (GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImageLayout = ImageLayout.Center; break;
case "Zoom" : (GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImageLayout = ImageLayout.Zoom; break;
case "Tile" : (GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImageLayout = ImageLayout.Tile; break;
case "Stretch" : (GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImageLayout = ImageLayout.Stretch; break;
case "None" : (GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImageLayout = ImageLayout.None; break;
}
}
//getting exception here. I set a small resizing size just for testing if it works. it doesn't
System.Drawing.Image img = resizeImage(System.Drawing.Image.FromFile(openFileDialog1.FileName), new Size(100, 100));
(GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImage = img;
}
}
}
}
因此,问题是,如何加载图像?
OutOfMemoryException是System.Drawing.Image.FromFile拒绝可卸载文件(例如,损坏的文件或不受支持的像素格式等)的典型方式。该异常非常令人困惑,可能与GDI +有关。 文件的大小根本不重要。
请参阅此MSDN页面上的异常部分: http : //msdn.microsoft.com/zh-cn/library/stf701f5.aspx
如果问题是在单个图像上发生的,则可以尝试使用更宽松的图形设计工具(如果可以打开!)来重新保存图像,或者可以尝试使用本机.Net方法之外的其他图片处理库。不依赖GDI +。
AFAIK,在大多数情况下,发生异常时,没有直接简单的解决方案可以“强制”加载图片。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.