如何解决内存不足异常
| 我具有创建动画gif的功能,它始终可以完美运行,但是现在当我所有的gif均为黑白时,它会显示OutOfMemory Exception On: e.AddFrame(Image.FromFile(imageFilePaths [i])); 我的职能:public void MakeGif(string sourcefolder,string destinationgif)
{
Isgifing = true;
string path = MainForm.RootDirectory;
String[] imageFilePaths = Directory.GetFiles(path);
String outputFilePath = MainForm.RootDirectory + @\"\\Final.gif\";
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.Start(outputFilePath);
e.SetDelay(300);
//-1:no repeat,0:always repeat
e.SetRepeat(0);
for (int i = 0,count = imageFilePaths.Length; i < count; i++)
e.AddFrame(Image.FromFile(imageFilePaths[i]));
e.Finish();
Isgifing = false;
}
AddFrame函数:
public bool AddFrame(Image im)
{
if ((im == null) || !started)
{
return false;
}
bool ok = true;
try
{
if (!sizeSet)
{
// use first frame\'s size
SetSize(im.Width,im.Height);
}
image = im;
GetimagePixels(); // convert to correct format if necessary
AnalyzePixels(); // build color table & map pixels
if (firstFrame)
{
WriteLSD(); // logical screen descriptior
WritePalette(); // global color table
if (repeat >= 0)
{
// use NS app extension to indicate reps
WritenetscapeExt();
}
}
WriteGraphicCtrlExt(); // write graphic control extension
WriteImageDesc(); // image descriptor
if (!firstFrame)
{
WritePalette(); // local color table
}
WritePixels(); // encode and write pixel data
firstFrame = false;
}
catch (IOException e)
{
ok = false;
}
return ok;
}
解决方法
Image.FromFile的文档说,如果文件不包含有效的图像,或者该图像采用GDI +不支持的格式,它将抛出“ 2”。
如果将代码重写为:
for (int i = 0,count = imageFilePaths.Length; i < count; i++)
{
var img = Image.FromFile(imageFilePaths[i]);
e.AddFrame(img);
}
如果调用Image.FromFile
时出现异常,那是因为无法加载图像。
, 我不知道详细信息,但是看起来并不写。没有“用法”,因此您可能不会处理资源。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。