我尝试使用 Bitmap.GetThumbnailImage() 函数为文件夹中的 20 多张图像生成缩略图。当应用程序执行以下过程时,我可以看到应用程序出现了巨大的内存峰值(任务管理器内存使用量约为 600,000K)。
foreach (var image in ListOfImages)
{
var thumbnailFolder = @"\thumb";
var thumbnailFile = thumbnailFolder + "\\" + image.Name;
if (!Directory.Exists(thumbnailFolder))
{
Directory.CreateDirectory(thumbnailFolder);
}
if (!File.Exists(thumbnailFile))
{
using (FileStream fs = new FileStream(image.FullName, FileMode.Open, FileAccess.Read))
{
Image origImage = Image.FromStream(fs);
var thumbnail = origImage.GetThumbnailImage(90, 120, null, IntPtr.Zero);
thumbnail.Save(thumbnailFile);
thumbnail.Dispose();
origImage.Dispose();
}
}
}
有什么方法可以减少缩略图生成的内存使用量吗?
最佳答案
尝试使用 WPF。
根据我的经验,WPF 的图像操作经过了很好的优化(实际上它是正在使用的 WIC 库),并且在设计时考虑到了线程,并且它不像 GDI+ 那样依赖于 GDI 位图句柄。我读过一次服务器代码不支持 GDI+,因为它并非完全无泄漏。对于您的方案,WPF 不需要 3D 视频卡。
WPF 的 BitmapDecoder
甚至具有内置的缩略图功能,如果可用,它将利用图像本身的缩略图。参见 http://msdn.microsoft.com/en-us/library/ms750864(VS.85).aspx用于 WPF 中的基本图像任务。要访问 WPF,您需要引用 WindowsBase 程序集(.net 3.0 或更高版本)。
关于c# - 创建文件夹内所有图像的缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2291415/