Imagefrom.File().内存不足异常 [英] Imagefrom.File(). Out of memory exception
问题描述
我正在尝试在列表中加载大约 60 张图片.每张图片大约1MB.对于 20 张图片没问题,但在此之上我在下面的代码行中出现内存不足异常.我搜索了大量相关问题,其中一些说明了使用"关键字和流,但由于我是初学者,有人可以帮助我.
I am trying to load about 60 pictures in a list. Each picture is about 1MB. For 20 pictures no problem but above that I get Out of memory exception on the code line below. I have searched vastly of related issues, some stating about "using" key word and stream but since I am a beginner can someone please help me.
Image image = Bitmap.FromFile(Filename);
这是我的代码
private void LoadBtn_Click_1(object sender, EventArgs e)
{
OpenFileDialog newDialog = new OpenFileDialog();
if (newDialog.ShowDialog() == DialogResult.OK)
{
images.Clear();
string dirPath = System.IO.Path.GetDirectoryName(newDialog.FileName.ToLower());
DirectoryInfo di = new DirectoryInfo(dirPath);
FileInfo[] finfos = di.GetFiles("*.*");
foreach (FileInfo fi in finfos)
{
string ext = fi.Extension.ToLower();
if ((ext.Equals(".png")) || (ext.Equals(".jpg")) || (ext.Equals(".tif")) || (ext.Equals(".gif")))
{
string Filename = fi.FullName;
Image image = Bitmap.FromFile(Filename); //exception occurs HERE
images.Add(image);
//this.imageList1.Images.Add(image);
//image.Dispose();
}
}
}
pictureBox3.Image = images[0];
}
我正在使用 C#、windows 窗体.谢谢
I am using C#, windows forms. thanks
推荐答案
看了你提到的软件后,我告诉过你,如果你只需要一个缩略图,你不需要将整个图像加载到内存中.
After taking a look at the software you mentioned, as I told you you don't need to load the whole image in memory if you only need a thumbnail.
所以我会创建 I 类
class ImageAndThumb
{
public Image Thumb;
public Image Big;
private string ImagePath;
public ImageAndThumb(string fileName)
{
ImagePath = fileName;
Image image = Image.FromFile(fileName)
Image thumb = img.GetThumbnailImage(200, 200, ()=>false, IntPtr.Zero);
}
public Image LoadBigImage()
{
Big = Image.FromFile(ImagePath);
return Big;
}
public void UnloadImage()
{
Big = null;
}
}
现在我们使用那个类:
List<ImageAndThumb> Images = new List<ImageAndThumb>();
private void LoadBtn_Click_1(object sender, EventArgs e)
{
OpenFileDialog newDialog = new OpenFileDialog();
if (newDialog.ShowDialog() == DialogResult.OK)
{
Images.Clear();
string dirPath = System.IO.Path.GetDirectoryName(newDialog.FileName.ToLower());
DirectoryInfo di = new DirectoryInfo(dirPath);
FileInfo[] finfos = di.GetFiles("*.*");
foreach (FileInfo fi in finfos)
{
string ext = fi.Extension.ToLower();
if ((ext.Equals(".png")) || (ext.Equals(".jpg")) || (ext.Equals(".tif")) || (ext.Equals(".gif")))
{
string Filename = fi.FullName;
ImageAndThumb image = new ImageAndThumb(Filename);
Images.Add(image);
}
}
}
pictureBox3.Image = Images[0].Thumb; // << Much less memory usage;
}
现在每当您需要使用图像时首先加载它例如:
And now whenever you need to use an image load it first For example:
void ShowPicture(int index)
{
Images[index].LoadBigImage();
PictureBoxBig.image = Images[index].Big;
}
void ClosePicture(int index)
{
Images[index].UnloadImage();
}
一个好主意是在加载另一个图像后卸载图像:
one good idea is to unload an image once you load another:
int currentPictureIndex = -1;
void ShowPicture(int index)
{
Images[index].LoadBigImage();
PictureBoxBig.image = Images[index].Big;
if(CurrentPictureIndex > -1) ClosePicture(CurrentPictureIndex);
currentPictureIndex = index;
}
这篇关于Imagefrom.File().内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!