WPF BitmapImage Width / Height Always 1?

I do not understand...

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

Exit: "Width: 1, Height: 1".

I tried PixelWidth / PixelHeight, I tried to manually create it with BeginInit/ EndInitand also set PreservePixelFormat... nothing works.

(Also, it’s even weirder: this is all part of the process when the user clicks a button and some images are downloaded. Well, the second time this button is clicked, it has a non-1 width / height.)

+3
source share
1 answer

The first time the user clicks the button, the bitmap is not yet loaded - so everything you do with it will produce garbage results (except for displaying it because the image control knows how to handle it).

BitmapImage.DownloadCompleted, , .

+4

Source: https://habr.com/ru/post/1698726/


All Articles