locked
How to change DIU width/height of a png image RRS feed

  • Question

  • Hi,

    I have a png image of 1275 X 1650 resolution and 32 bit depth, but when it is loaded in BitMapImage its Width & height are returned to a very low value 408 X 528. I have a similar png image of same resolution and bit depth but it returns a higher Width & Height (816 X 1056) after it is loaded in BitMapImage object. Can you please tell how/what to change in the png image to get a better Width & Height after it is loaded in BitMapImage class.

    Regards,

    R.Sundar

    Thursday, December 12, 2013 12:57 PM

Answers

  • Hi,

    I create a bitmap image object on my end and load a local PNG file, if the image resolution is 1920*1080, 64bit. When I load this image in an image control. It has the same size. If the image is 8bit, I noticed that the size is not equivalent to its resolution.

    Here is my test code:

    C# Code:

    	// Create the image element.
    	Image simpleImage = new Image();
    
    	// Create source.
    	BitmapImage bi = new BitmapImage();
    	// BitmapImage.UriSource must be in a BeginInit/EndInit block.
    	bi.BeginInit();
    	bi.UriSource = new Uri("D:/Media/png1.png", UriKind.Absolute);
    
    	bi.EndInit();
    	// Set the image source.
    	simpleImage.Source = bi;

    I thought bitmap image size has relationship with image’s bits depth.

    >>Can you please tell how/what to change in the png image to get a better Width & Height after it is loaded in Bit Map Image class.<<

    In my view, we don’t need to restrict the bitmap’s size. We can restrict the image container’s size. In general, we can scale the image container’s size to suit the bitmap size.

    Have a nice time!

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 13, 2013 11:48 AM
  • "have a png image of 1275 X 1650 resolution and 32 bit depth, but when it is loaded in BitMapImage its Width & height are returned to a very low value 408 X 528."

    Let's make some calculations. 1275 / 408=3.125 and so is 1650 / 528. Where does 3.125 comes from? 96 * 3.125 = 300. That is, your png resolution is 300dpi (probably it's a scanned image) but WPF uses 96dpi and the Width/Height properties account for that.

    "Can you please tell how/what to change in the png image to get a better Width & Height after it is loaded in BitMapImage class."

    That depends a lot on why you need this.

    Do you want to know the image size in pixels? Then use the PixelWidth and PixelHeight properties instead of Width & Height.

    Do you want to display the image as if its resolution is 96 instead of 300? There are at least 2 ways to do this.

    One way is to modify the image with an image editing program, for example Paint.NET allows you to change the resolution of an image.

    Another way is to use a TransformedBitmap to undo the scaling done by WPF. For example:

    var image = new BitmapImage();
    image.BeginInit();
    image.UriSource = new Uri(@"C:\users\mike\desktop\untitled.png");
    image.EndInit();
    var scaled = new TransformedBitmap(image, new ScaleTransform(
        image.DpiX / 96.0,
        image.DpiY / 96.0));
    img.Source = scaled;
    

    Friday, December 13, 2013 4:17 PM

All replies

  • Hi,

    I create a bitmap image object on my end and load a local PNG file, if the image resolution is 1920*1080, 64bit. When I load this image in an image control. It has the same size. If the image is 8bit, I noticed that the size is not equivalent to its resolution.

    Here is my test code:

    C# Code:

    	// Create the image element.
    	Image simpleImage = new Image();
    
    	// Create source.
    	BitmapImage bi = new BitmapImage();
    	// BitmapImage.UriSource must be in a BeginInit/EndInit block.
    	bi.BeginInit();
    	bi.UriSource = new Uri("D:/Media/png1.png", UriKind.Absolute);
    
    	bi.EndInit();
    	// Set the image source.
    	simpleImage.Source = bi;

    I thought bitmap image size has relationship with image’s bits depth.

    >>Can you please tell how/what to change in the png image to get a better Width & Height after it is loaded in Bit Map Image class.<<

    In my view, we don’t need to restrict the bitmap’s size. We can restrict the image container’s size. In general, we can scale the image container’s size to suit the bitmap size.

    Have a nice time!

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 13, 2013 11:48 AM
  • "have a png image of 1275 X 1650 resolution and 32 bit depth, but when it is loaded in BitMapImage its Width & height are returned to a very low value 408 X 528."

    Let's make some calculations. 1275 / 408=3.125 and so is 1650 / 528. Where does 3.125 comes from? 96 * 3.125 = 300. That is, your png resolution is 300dpi (probably it's a scanned image) but WPF uses 96dpi and the Width/Height properties account for that.

    "Can you please tell how/what to change in the png image to get a better Width & Height after it is loaded in BitMapImage class."

    That depends a lot on why you need this.

    Do you want to know the image size in pixels? Then use the PixelWidth and PixelHeight properties instead of Width & Height.

    Do you want to display the image as if its resolution is 96 instead of 300? There are at least 2 ways to do this.

    One way is to modify the image with an image editing program, for example Paint.NET allows you to change the resolution of an image.

    Another way is to use a TransformedBitmap to undo the scaling done by WPF. For example:

    var image = new BitmapImage();
    image.BeginInit();
    image.UriSource = new Uri(@"C:\users\mike\desktop\untitled.png");
    image.EndInit();
    var scaled = new TransformedBitmap(image, new ScaleTransform(
        image.DpiX / 96.0,
        image.DpiY / 96.0));
    img.Source = scaled;
    

    Friday, December 13, 2013 4:17 PM