2

I've a little issue over here. I'm resizing the image using the following code snippet:

public static BitmapFrame FastResize(BitmapFrame image, int width, int height) {
    var scaleX = width / image.Width * 96 / image.DpiX;
    var scaleY = height / image.Height * 96 / image.DpiY;
    var target = new TransformedBitmap(image, new ScaleTransform(scaleX, scaleY, 0, 0));
    return BitmapFrame.Create(target);
}

The problem is when I'm calculating the value of scaleX and scaleY. Some of the image has DPI, but some of them has 0.00 or I can assume the DPI is not set for those images. And as a result, I'm having value Infinity for those two scaleX and scaleY variables. Because the value of image.DpiX and image.DpiY are 0.00. Well, it is not throwing Attempt to divide by zero exception.

In order to achieve a high and expected quality after the image scaling/resizing process, I need to calculate the scaleX and scaleY in that way: width / image.Width * 96 / image.DpiX. However, as I've already mentioned, some of the images doesn't have DPI set.

So my question is what should I do in this case if the image has no DPI set, in order to prevent having a value Infinity for those variables? Is there a default DPI value we can use for images if it is not set? Or may be a way of calculating PDI programmatically using WPF/WIC (WPF and Windows Imaging Component) in case even if it is not set?

user3332579
  • 3,111
  • 3
  • 17
  • 21

1 Answers1

1

Check if all your images are pixel based. Vector based images don't have DPI.

GregoryHouseMD
  • 2,168
  • 1
  • 21
  • 37
  • Thanks for reply. Well, my program doesn't care if the image is pixel or vector based. It needs to scale any provided image. Let's say the image is vector based, then how can I calculate the value of `scaleX` and `scaleY` variables in this case? – user3332579 Mar 20 '14 at 20:56
  • 1
    There is your problem then, you have to check if the image is vector od pixel first, read [this](http://stackoverflow.com/questions/4941822/how-can-i-resize-an-image-in-c-sharp-while-retaining-high-quality) description about the differences between vector and pixel images, it shuold clear things up – GregoryHouseMD Mar 20 '14 at 21:06
  • can a Jpeg image be vector based? I've just checked one image, which is jpeg, and doesn't have DPI set. The image is taken using iPhone. – user3332579 Mar 20 '14 at 21:27
  • There are a lot of parameters in play when you're accepting all types of images, so I'd suggest you read up on all of them. [JPEG doesn't need DPI](http://blog.forret.com/2008/04/a-jpeg-picture-doesnt-care-about-no-dpi/) for instance, so maybe use a switch and depending on the type of image, handle the scaling differently. – GregoryHouseMD Mar 20 '14 at 21:46