Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BitmapImage returns 0 for PixelWidth and PixelHeight

I'm trying to do a super simple thing : get the size of an image.

So I create my BitmapImage but I'm getting 0 when I try to access the PixelWidth and PixelHeight.

How can I accomplish that please?

EDIT: (added sample code)

I'm just doing:

var baseUri = new Uri("ms-appx:///");
var bitmapImage = new BitmapImage(new Uri(baseUri, "Assets/Logo.png"));

MyImage.Source = bitmapImage;

Debug.WriteLine("Width: " + bitmapImage.PixelWidth + " Height: " + bitmapImage.PixelHeight);

In the console, I get:

Width: 0 Height: 0
like image 712
Antoine Gamond Avatar asked Mar 14 '13 10:03

Antoine Gamond


1 Answers

When you want to use the image properties after setting the source for your BitmapImage, you normally have to write an event handler that will execute on ImageOpened.

Also remember that ImageOpened only fires if the image is downloaded and decoded (i.e. using Image.Source).

var bitmapImage = new BitmapImage(uri);
bitmapImage.ImageOpened += (sender, e) => 
{
    Debug.WriteLine("Width: {0}, Height: {1}",
        bitmapImage.PixelWidth, bitmapImage.PixelHeight);
};
image.Source = bitmapImage;
like image 187
Paolo Moretti Avatar answered Oct 28 '22 11:10

Paolo Moretti