user3332579
user3332579

Reputation: 3201

How to find image's DPI if it is not set, WPF/WIC?

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?

Upvotes: 2

Views: 536

Answers (1)

GregoryHouseMD
GregoryHouseMD

Reputation: 2378

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

Upvotes: 1

Related Questions