I'd like to make thumbnails from a byte[] image that came from a Stream. I have two options, DecodePixelWidth or ScaleTransform.
My Questions:
First:
I prefer this method. It uses slightly more memory but seems to be faster. However, Idk why? Is it using Matrix and using the GPU to do the work? In which case my customer may or may not be able to do it as fast as I can.
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;
}
Second:
This method uses less memory but seems slower and the images are blurry but they're thumbnails so it's okay. Still, is ScaleTransform better?
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;
}
Thank you for your help.
After some research I came to this conclusion.
ScaleTransform:
According to this, ScaleTransform uses the transformation matrix to calculate the points. It also has features such as
Freezable Features: ScaleTransform objects can be declared as resources, shared among multiple objects, made read-only to improve performance, cloned, and made thread-safe.
You can also rotate, flip, create mirror images and more with ScaleTransform unlike DecodePixelWidth. Look at these examples.
When to use:
When not to use:
DecodePixelWidth:
The DecodePixelWidth is another option to resize images. The only issue is that it seems to only deal with JPEG/PNG Codecs.
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.
In fact, it would result in odd behavior if you try to use it with other types of images. You'd be better off modifying the width in XAML. Furthermore, it will distort your images if you use formats other than the JPEG/PNG Codecs. It seems, to me, that since in other formats it will decode the image at it's original size it will most likely put the pixels too close together and distort the image.
When to use:
When not to use:
Conclusion:
They're just two different ways of resizing the images except that ScaleTransform has other features and is the better option to go with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With