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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With