直接从无标题的图像字节数组创建缩略图 [英] Create thumbnail image directly from header-less image byte array

查看:96
本文介绍了直接从无标题的图像字节数组创建缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序一次显示大量图像缩略图.目前,我将所有全尺寸图像保留在内存中,并只是在UI中缩放图像以创建缩略图.但是,我宁愿只在内存中保留小缩略图,并且仅在必要时才加载完整尺寸的图像.

My application shows a large number of image thumbnails at once. Currently, I am keeping all full size images in memory, and simply scaling the image in the UI to create the thumbnail. However, I'd rather just keep small thumbnails in memory, and only load the fullsize images when necessary.

我认为这很容易,但是与仅在UI中缩放全尺寸图像相比,我生成的缩略图非常模糊.

I thought this would be easy enough, but the thumbnails I'm generating are terribly blurry compared to just scaling the fullsize image in the UI.

图像是没有标题信息的字节数组.我提前知道了大小和格式,因此可以使用BitmapSource.Create来创建ImageSource.

The images are byte arrays with no header information. I know the size and format ahead of time, so I can use BitmapSource.Create to create an ImageSource.

 //This image source, when bound to the UI and scaled down creates a nice looking thumbnail
 var imageSource = BitmapSource.Create(
     imageWidth,
     imageHeight,
     dpiXDirection,
     dpiYDirection,
     format,
     palette,
     byteArray,
     stride);

using (var ms = new MemoryStream())
{
    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(imageSource);
    encoder.Save(ms);

    var bi = new BitmapImage();
    bi.BeginInit();
    bi.CacheOption = BitmapCacheOption.OnLoad;

    //I can't just create a MemoryStream from the original byte array because there is no header info and it doesn't know how to decode the image!
    bi.StreamSource = ms;
    bi.DecodePixelWidth = 60;
    bi.EndInit();

    //This thumbnail is blurry!!!
    Thumbnail = bi;
}

我正在猜测它的模糊性,因为我先将其转换为png,但是当我使用BmpBitmapEncoder时,出现没有可用的成像组件"错误.在这种情况下,我的图像是Gray8,但是我不确定为什么PngEncoder可以理解它,而BmpEncoder不能.

I'm guessing its blurry since I'm converting it to png first, but when I use the BmpBitmapEncoder I get that "No imaging component available" error. In this case my image is Gray8, but I'm not sure why the PngEncoder can figure it out but the BmpEncoder can't.

肯定有某种方法可以从原始ImageSource创建缩略图,而不必先将其编码为位图格式吗?我希望BitmapSource.Create可以让您像BitmapImage类那样指定解码宽度/高度.

Surely there must be someway to create a thumbnail from the original ImageSource without having to encode it to a bitmap format first? I wish BitmapSource.Create just let you specify a decode width/height like the BitmapImage class does.

编辑

最后的答案是使用带有WriteableBitmap的TransformBitmap来创建缩略图并消除原始的全尺寸图像.

The final answer is to use a TransformBitmap with a WriteableBitmap to create the thumbnail and eliminate the original, full-size image.

var imageSource = BitmapSource.Create(...raw bytes and stuff...);
var width = 100d;
var scale = width / imageSource.PixelWidth;
WriteableBitmap writable = new WriteableBitmap(new TransformedBitmap(imageSource, new ScaleTransform(scale, scale)));
writable.Freeze();

Thumbnail = writable;

推荐答案

您应该能够创建

You should be able to create a TransformedBitmap from the original one:

var bitmap = BitmapSource.Create(...);
var width = 60d;
var scale = width / bitmap.PixelWidth;
var transform = new ScaleTransform(scale, scale);
var thumbnail = new TransformedBitmap(bitmap, transform);

更新:为了最终摆脱原始的位图,您可以从TransformedBitmap创建一个WriteableBitmap:

UPDATE: In order to get ultimately rid of the original bitmap, you could create a WriteableBitmap from the TransformedBitmap:

var thumbnail = new WriteableBitmap(new TransformedBitmap(bitmap, transform));

这篇关于直接从无标题的图像字节数组创建缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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