我不知道问题到底是什么。我可以加载小的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;
}
}
}
}
所以,问题是,如何加载图像?
答案 0 :(得分:4)
OutOfMemoryException是System.Drawing.Image.FromFile拒绝无法加载文件的典型方式,例如损坏的文件或不支持的像素格式等。 例外很混乱,可能与GDI +有关。 文件的大小根本不重要。
请参阅此MSDN页面上的例外部分:http://msdn.microsoft.com/en-us/library/stf701f5.aspx
如果单个图像出现问题,您可以尝试使用更宽松的图形设计工具重新保存图像(如果可以打开它!),或者您可以尝试使用其他图片处理库而不是本机.Net方法,它不依赖于GDI +。
AFAIK,大多数情况下发生异常时没有直接的简单解决方案来强制"强迫"加载图片。