Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF BitmapImage Width/Height are always 1?

I don't understand...

BitmapImage img = new BitmapImage(myUri);
Console.WriteLine("Width: {0}, Height: {1}", img.Width, img.Height);

Output: "Width: 1, Height: 1".

I've tried PixelWidth/PixelHeight, I've tried manually creating it with BeginInit/EndInit and also setting PreservePixelFormat... nothing works.

(Except, even wierder: this is all part of a process where the user clicks a button and some images get downloaded. Well, the second time that button is clicked, it does have non-1 width/height.)

like image 292
Domenic Avatar asked Sep 06 '25 03:09

Domenic


1 Answers

The first time the user clicks the button the bitmap hasn't been downloaded yet - so anything you do with it will cause garbage results (except displaying it, because the Image control knows how to handle this).

You can handle the BitmapImage.DownloadCompleted event to know when the bitmap is available.

like image 144
Nir Avatar answered Sep 09 '25 03:09

Nir