以最小延迟快速显示超高分辨率图像的缩略图 [英] Displaying Thumbnails of very high resolution images Fast with Minimal Delay

查看:35
本文介绍了以最小延迟快速显示超高分辨率图像的缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在控件中显示高分辨率图像的预览缩略图以供用户选择.我目前使用 ImageListView 加载图像.这适用于中低分辨率图像.但是当显示非常高分辨率图像的缩略图时,会有明显的延迟.示例图像可以从 https://drive.google.com/open?id=1Qgu_aVXBiMlbHluJFU4fBvmFC45-E81C

I need to show the preview thumbnails of high resolution images in a control for user selection. I currently use ImageListView to load images. This works fine for low to medium resolution images.But when it comes to showing thumbnails of very high resolution images there is a noticeable delay.Sample image can be downloaded from https://drive.google.com/open?id=1Qgu_aVXBiMlbHluJFU4fBvmFC45-E81C

图像大小约为 5000x3000 像素,大小约为 12 MB.使用此图像的 1000 个副本可以复制该问题.

The image size is around 5000x3000 pixels and size is around 12 MB.The issue can be replicated by using 1000 copies of this image.

问题屏幕截图在此处上传

The issue screen capture is uploaded here

https://giphy.com/gifs/ZEH3T3JTfN42OL3J1A

图像是使用后台工作者加载的

The images are loaded using a background worker

foreach (var f in filepaths)
{
    imageListView1.Items.Add(f);              
}

1.为了解决这个问题,我尝试调整大分辨率图像的大小并将调整后的图像添加到 ImageListView ......但是调整大小会消耗大量时间并且缩略图生成速度很慢.

1. In order to solve this issue I tried resizing large resolution images and adding the resized image to ImageListView ... but for resizing there is a heavy time consumption and thumbnail generation is slow.

Bitmap x = UpdatedResizeImage2(new Bitmap(f), new Size(1000, 1000));
string q = Path.GetTempPath() + Path.GetFileName(f);
x.Save(Path.GetTempPath() + Path.GetFileName(f));
x.Dispose();
imageListView1.Items.Add(Path.GetTempPath() + Path.GetFileName(f));

2. 我也试过 Image.CreateThumbnail 方法,但这也很慢.

2. I have also tried Image.CreateThumbnail Method but this is also quite slow.

有没有更好的方法来解决这个问题?

Is there a better way to solve this issue?

推荐答案

您可以使用 WPF 互操作 并使用 DecodePixelWidth/Height 属性.他们使用底层 Windows 成像层技术("Windows Imaging Component") 以创建优化的缩略图,从而节省大量内存(可能还有 CPU):如何:使用位图图像 (XAML)

You could use WPF interop and use the DecodePixelWidth/Height properties. They use underlying Windows imaging layer technology ("Windows Imaging Component") to create an optimized thumbnail, saving lots of memory (and possibly CPU): How to: Use a BitmapImage (XAML)

您也可以通过代码使用 WPF/WIC,代码如下(改编自本文 从 ASP.NET 调整图像大小的最快方法.它是(更多)支持的.. 您只需要添加对 PresentationCore 和 WindowsBase 的引用,这对于桌面应用来说应该不是问题.

You can also use WPF/WIC by code, with a code like this (adapted from this article The fastest way to resize images from ASP.NET. And it’s (more) supported-ish.. You just need to add a reference to PresentationCore and WindowsBase which shouldn't be an issue for a desktop app.

    // needs System.Windows.Media & System.Windows.Media.Imaging (PresentationCore & WindowsBase)
    public static void SaveThumbnail(string absoluteFilePath, int thumbnailSize)
    {
        if (absoluteFilePath == null)
            throw new ArgumentNullException(absoluteFilePath);

        var bitmap = BitmapDecoder.Create(new Uri(absoluteFilePath), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None).Frames[0];
        int width;
        int height;
        if (bitmap.Width > bitmap.Height)
        {
            width = thumbnailSize;
            height = (int)(bitmap.Height * thumbnailSize / bitmap.Width);
        }
        else
        {
            width = (int)(bitmap.Width * thumbnailSize / bitmap.Height);
            height = thumbnailSize;
        }

        var resized = BitmapFrame.Create(new TransformedBitmap(bitmap, new ScaleTransform(width / bitmap.Width * 96 / bitmap.DpiX, height / bitmap.Height * 96 / bitmap.DpiY, 0, 0)));
        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(resized);
        var thumbnailFilePath = Path.ChangeExtension(absoluteFilePath, thumbnailSize + Path.GetExtension(absoluteFilePath));
        using (var stream = File.OpenWrite(thumbnailFilePath))
        {
            encoder.Save(stream);
        }
    }

否则有很多工具,如 MagicScaler、FreeImage ImageSharp、ImageMagick、Imazen 等.大多数是为 ASP.NET/Web 服务器场景编写的(官方不支持 WPF 但可以使用,请阅读文章)和也是您似乎不需要的跨平台.我不确定它们通常比内置的 Windows 技术更快或使用更少的内存,但您应该在您的上下文中测试所有这些.

Otherwise there are lots of tools out there like MagicScaler, FreeImage ImageSharp, ImageMagick, Imazen, etc. Most were written for ASP.NET/Web server scenarios (for which WPF is officially not supported but works, read the article) and are also cross-platform which you don't seem to need. I'm not sure they're generally faster or use less memory than builtin Windows technology, but you should test all this in your context.

PS:否则没有灵丹妙药,更大的图像需要更多时间.

PS: otherwise there's no magic bullet, bigger images take more time.

这篇关于以最小延迟快速显示超高分辨率图像的缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆