编程语言
首页 > 编程语言> > c# – DecodePixelWidth Vs.有什么区别? ScaleTransform?

c# – DecodePixelWidth Vs.有什么区别? ScaleTransform?

作者:互联网

我想从一个来自Stream的byte []图像制作缩略图.我有两个选项,DecodePixelWidth或ScaleTransform.

我的问题:

>哪一个更快?
>哪种方式比较合适?
>他们每个人做什么?
>哪一个使用更少的内存/ CPU?

第一:

我更喜欢这种方法.它使用稍多的内存,但似乎更快.但是,Idk为什么?它是使用Matrix并使用GPU来完成工作吗?在这种情况下,我的客户可能会或可能不会尽快完成.

using (var stream = new MemoryStream(rasterizedPage.ImageData, false))
{
    var bitmap = DocHelper.ConvertToBitmapImage(stream);
    var transform = new ScaleTransform(0.1, 0.1);
    var thumbnail = new WriteableBitmap(new TransformedBitmap(bitmap, transform));

    byte[] byteImage = DocHelper.ConvertToBytes(thumbnail);

    return byteImage;
}

第二:

这种方法使用较少的内存,但似乎较慢,图像模糊,但它们是缩略图,所以没关系.还是,ScaleTransform更好吗?

using (var stream = new MemoryStream(rasterizedPage.ImageData, false))
{
    byte[] byteImage;
    var bitmap = new BitmapImage();
    bitmap.BeginInit();
    bitmap.DecodePixelWidth = 120;
    bitmap.StreamSource = stream;
    bitmap.EndInit();
    bitmap.Freeze();
    byteImage = DocHelper.ConvertToBytes(bitmap);

    return byteImage;
}

谢谢您的帮助.

解决方法:

经过一些研究,我得出了这个结论.

ScaleTransform:

根据this,ScaleTransform使用transformation matrix来计算积分.它还具有诸如此类的功能

Freezable Features: ScaleTransform objects can be declared as resources, shared among multiple objects, made read-only to improve
performance, cloned, and made thread-safe.

与DecodePixelWidth不同,您还可以使用ScaleTransform旋转,翻转,创建镜像等.看看这些examples.

何时使用:

>旋转图像.
>调整图像大小.
>翻转图像.
>创建镜像.
>使用线程的应用程序.
>使用图像作为资源.

何时不使用:

>使图像太大.它会破裂.您的应用程序将使用如此多的内存,您将获得内存异常.看看here.

DecodePixelWidth:

DecodePixelWidth是另一种调整图像大小的选项.唯一的问题是它似乎只处理JPEG / PNG编解码器.

The JPEG and Portable Network Graphics (PNG) codecs natively decode
the image to the specified size; other codecs decode the image at its
original size and scale the image to the desired size.

实际上,如果您尝试将其与其他类型的图像一起使用,则会产生odd behavior.你最好在XAML中修改宽度.此外,如果您使用JPEG / PNG编解码器以外的格式,则会显示distort图像.对我来说,似乎在其他格式中,它会以原始大小对图像进行解码,因此很可能会使像素太靠近并使图像失真.

何时使用:

> JPEG / PNG编解码器内的图像.
>将大图像调整为小图像以节省内存.

何时不使用:

>当您的图像使用与JPEG / PNG不同的编解码器时.

结论:

它们只是调整图像大小的两种不同方式,除了ScaleTransform具有其他功能,并且是更好的选择.

标签:c,net,pixel,decode,scaletransform
来源: https://codeday.me/bug/20190628/1318757.html