Changing the DPI of an image ? RRS feed

  • Question


    Hi guys.  Thanks for all the help I've recieved here so far.


    My program displays and manipulates images, so I've already worked out that for it to work properly, all my images need to be at the same DPI ( because if I use scale transforms to correct for DPI, then I need to change all my other tool code ).  The problem is that I want my users to be able to import their own images.  I know there are commerical tools that change the DPI of an image, and I know it's just metadata on the image.  So, what I need to do, is figure out a way to change the DPI of the image, either in memory, or on the hard drive.



    Sunday, September 30, 2007 9:28 PM

All replies


    The DecodePixelWidth and DecodePixelHeight properties of BitmapImage are what you're looking for.  I posted some sample code in this earlier post.  I also put a more complete sample together as described here.
    Monday, October 1, 2007 12:13 AM

    I appreciate the reply, but that doesn't answer my question at all.


    1 - the BitmapImage class is buggy, for some images, at some sizes, it totally destroys them when it resizes them

    2 - the image size is under control, and irrelevant to my question.  If the DPI of two images is different, but they have the same pixel size, WPF will show them at different sizes.  WPF respects DPI, which until now as been metadata that is totally ignored, opening up a pandoras box for anyone trying to view images that have been created in the past, or from disparate sources ( I found some that had a DPI of 6000, which could only mean the DPI setting was correct for the tiny viewfinder in the digital camera that took them ).  So, unless I find a way to set the DPI, I cannot import images, because the size becomes irrelevant, once DPI is taken into account, they will show up in all sorts of sizes, the ActualPixelWidth and ActualPixelHeight is a combination of your display DPI, the image DPI, and the number of pixels in the actual image.


    Monday, October 1, 2007 12:24 AM
  • Hi Christian,

    If you want to do this without using any of the WPF imaging classes, which all seem to demonstrate that same resampling error you've been getting, there's an excellent article on image resampling using .NET 2.0 and some custom filters over at Code Project:


    Not everything needs to be done in a WPF way Smile  Particularly if there's a superior solution using the old classes.  I spent quite a bit of time trying to figure out a way to do this using .NET 3+ classes but I can't find a good one.

    Let me know if his article helps.  I may need it myself.
    Monday, October 1, 2007 2:42 AM

    Cool article, thanks.  I have an article on image resizing on CP, but it just demonstrates a Bilinear filter, and was just a feeder for an image processing article.  This one is much better.


    I'm using the TransformedBitmap class to resize images now, and it's not given us the problem we were getting before, you're saying it does give that issue ? I moved briefly to using GDI+ to resize my images, but it was just too slow, which is why I ended up with the TransformedBitmap class.


    I'm not sure that resizing the bitmaps will solve my problem ( although it's worth exploring ), I'd really like to be able to change that DPI setting.  I was in fact expecting I would not use WPF, but I thought I'd ask here first, given that prior to WPF, DPI was not an issue, I expected WPF to be the place that SHOULD have the support for changing it.


    Thanks for helping



    Monday, October 1, 2007 3:49 AM
  • I'm using the TransformedBitmap class to resize images now, and it's not given us the problem we were getting before, you're saying it does give that issue ?

    I didn't try that class.  I started from the presumption that since BitmapImage is used so pervasively in the Imaging framework that you'd probably run into it with most of the imaging classes you tried to use.  The one option I saw was to attempt to re-write the image using one of the Encoder classes (PngImageEncoder, for example) but I couldn't find a good way to re-render the image from the source image.

    So, instead, I looked for GDI+ solutions since I knew I'd find one there.  I pointed you to that article specifically because it contains resampling algorithms to change the DPI.  I didn't actually try to use it though.
    Monday, October 1, 2007 4:19 AM

    Oh, I've found it in the forum below, he says that SetResolution will fix it, I will check that out, thanks.



    Monday, October 1, 2007 5:01 AM
  • Mea culpa, Christian.  I read your question without giving it the attention it deserved.  Rarely is "dpi" used here to actually mean dpi (or so it seems).  Clearly, I should have at least glanced at the prior posts to which you referred.


    Yes, I agree that it would have been nice to at least see the entry points for true resolution scaling... especially since WIC is the only public entry point into the unmanaged portion of the platform in v1.  If you're going to expose an interface for third parties to plug in their imaging libraries, you might as well let them provide resampling functionality too.  Maybe they thought they would need to at least provide a rudimentary set of resampling filters for the native encoders if they did this.


    This is very much a v1 product and I only expect it to get better in the future.  In addition to new features, hopefully a lot of the issues you've encountered thus far will become non-issues.

    Monday, October 1, 2007 8:42 AM

    That's cool.  I think you're right, WPF will only get better, it's those of us who are using v1 to write big apps who are exposing the flaws and ( hopefully ) providing the feedback that will guide Microsoft in improving it in the future.


    Monday, October 1, 2007 8:47 AM