c# - 创建文件夹内所有图像的缩略图

标签 c# performance gdi+

我尝试使用 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/

相关文章:

c++ - 如何使用 int 变量绘制 DrawString

c# - 如何避免在 RX 中使用 Subjects

c# - 对于新的 .net 核心应用程序,我应该在 protobuf-net 和 google.protobuf 之间使用什么 NuGet 包?

mysql - 我应该在SqlAlchemy中使用relationship()吗?

performance - 网站用户事件日志数据存储的建议

c# - Alpha channel 透明度和调整图像文件大小

c# - 我如何使用 .NET ColorMatrix 更改颜色?

c# - DateTime ConvertTimeBySystemTimeZoneId 不工作

c# - 以编程方式读取构建配置

python - 为什么 Python 中的局部变量访问比类成员访问更快?