none
Trying to load an image at actual size from a stream

    Question

  •  

    I've seen some replies on things like this in the forum, but none that solve my issue.  I have a stream, it contains a png file, or a jpg.  I was using the Width/Height property to make sure I never asked for a size greater than the image when I set the DecodePixelHeight or DecodePixelWidth properties.  I've realised this takes into account the DPI, and so I'm now using the PixelWidth or PixelHeight properties instead.  By that I mean, I set only one, so as to make sure I don't mess with the aspect ratio.

     

    My issue is simply that when I load an image into a BitmapImage and make it the Source of an Image, it uses the DPI setting to show the image at a tiny size.  I am getting around this by setting the scale, but it's not a perfect solution, is there any other way to do this ?

     

     

    Christian

    Monday, August 20, 2007 3:41 AM
    Moderator

Answers

  •  

    Hi Christian,

     

    The answer to your question depends on what you mean by "Actual Size".  From your statement, "... it uses the DPI setting to show the image at a tiny size...", I assume that you want WPF to display the image at the same screen size as Windows Photo Gallery does.

     

    Windows Photo Gallery ignores the DPI (dots a.k.a pixels per inch) of an image whereas WPF takes the DPI into account.  This means that if an image has a non-standard DPI (read: not 96dpi), that WPF and Windows Photo Gallery will display the image at different sizes on the screen.

     

    The fix that you are most likely looking for is to simply set the Image's width and height to your BitmapImage's PixelWidth and PixelHeight properties.

     

    The root of the confusion winds a bit deeper however.  Now for an example, and please note that I'll be assuming a 96dpi display.  Create a new WPF application and name your Window "_window1", now place the following code after the call to InitializeComponent() in the Window's constructor.

     

    Code Snippet

    Image image = new Image();

    Uri uri = new Uri("pack://application:,,,/test-image.jpg");

    BitmapImage bitmapImage = new BitmapImage(uri);

    image.Source = bitmapImage;

    image.MaxWidth = bitmapImage.Width;

    image.MaxHeight = bitmapImage.Height;

    _window1.Content = image;

     

    Consider test-image.jpg, a 400x400 image with a resolution of 400dpi.  WPF will see that the the image is 400 pixels wide, that you want 400 pixels per inch, and therefore that the image should be displayed one inch wide on the screen.  Since you have a 96dpi display, 96 pixels on your display are one inch wide, and the Image's ActualWidth property will be set to 96.  WPF will display this image 96 pixels wide since this is the actual size of the image.

     

    In contrast, Windows Photo Gallery will simply disregard the DPI and display the image 400 pixels wide.

     

    This concept can be kind of confusing, so I hope this explanation cleared things up for you.  Let me know if the fix that I proposed above doesn't work for you.

     

    -Mike

     

     

     

    Wednesday, August 29, 2007 9:24 PM

All replies

  •  

    Hi Christian,

     

    The answer to your question depends on what you mean by "Actual Size".  From your statement, "... it uses the DPI setting to show the image at a tiny size...", I assume that you want WPF to display the image at the same screen size as Windows Photo Gallery does.

     

    Windows Photo Gallery ignores the DPI (dots a.k.a pixels per inch) of an image whereas WPF takes the DPI into account.  This means that if an image has a non-standard DPI (read: not 96dpi), that WPF and Windows Photo Gallery will display the image at different sizes on the screen.

     

    The fix that you are most likely looking for is to simply set the Image's width and height to your BitmapImage's PixelWidth and PixelHeight properties.

     

    The root of the confusion winds a bit deeper however.  Now for an example, and please note that I'll be assuming a 96dpi display.  Create a new WPF application and name your Window "_window1", now place the following code after the call to InitializeComponent() in the Window's constructor.

     

    Code Snippet

    Image image = new Image();

    Uri uri = new Uri("pack://application:,,,/test-image.jpg");

    BitmapImage bitmapImage = new BitmapImage(uri);

    image.Source = bitmapImage;

    image.MaxWidth = bitmapImage.Width;

    image.MaxHeight = bitmapImage.Height;

    _window1.Content = image;

     

    Consider test-image.jpg, a 400x400 image with a resolution of 400dpi.  WPF will see that the the image is 400 pixels wide, that you want 400 pixels per inch, and therefore that the image should be displayed one inch wide on the screen.  Since you have a 96dpi display, 96 pixels on your display are one inch wide, and the Image's ActualWidth property will be set to 96.  WPF will display this image 96 pixels wide since this is the actual size of the image.

     

    In contrast, Windows Photo Gallery will simply disregard the DPI and display the image 400 pixels wide.

     

    This concept can be kind of confusing, so I hope this explanation cleared things up for you.  Let me know if the fix that I proposed above doesn't work for you.

     

    -Mike

     

     

     

    Wednesday, August 29, 2007 9:24 PM
  • I'm working on a similar issue, and having trouble.  I'm trying to display some images in WPF at their pixel size, i.e. ignoring dpi.  Setting the height and width of the images to PixelHeight and PixelWidth as you describe doesn't work...it seems to still be factoring dpi in.  Is there a way to ignore DPI as Windows Photo Gallery does?
    Monday, October 06, 2008 6:06 PM