Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between DecodePixelWidth Vs. ScaleTransform?

I'd like to make thumbnails from a byte[] image that came from a Stream. I have two options, DecodePixelWidth or ScaleTransform.

My Questions:

  1. Which one is faster?
  2. Which is the more appropriate way?
  3. What does each of them do?
  4. Which one uses less memory/CPU?

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.

like image 824
AzzamAziz Avatar asked Sep 28 '22 22:09

AzzamAziz


1 Answers

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:

  1. To rotate image.
  2. To resize image.
  3. To flip image.
  4. Create mirror images.
  5. Applications that use threading.
  6. Using images as resources.

When not to use:

  1. To make images too large. It will break. Your application will use so much memory you will get a memory exception. Take a look here.

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:

  1. Images that are within the JPEG/PNG Codecs.
  2. To resize large images into small images to save memory.

When not to use:

  1. When your image uses a different Codecs than JPEG/PNG.

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.

like image 59
AzzamAziz Avatar answered Oct 05 '22 06:10

AzzamAziz