I am trying to get the height of an image like the following
BitmapImage bitmap = new BitmapImage(new Uri("Images/header.png",UriKind.RelativeOrAbsolute));
Debug.WriteLine("bitmap.PixelHeight : " + bitmap.PixelHeight);
It is printing the height as 0, but the actual image's height is 35 pixel开发者_运维知识库s. It is not an image loaded from server. It is stored inside the application bundle itself.
Do you use your bitmap later in your project? If you are using it as Source in Image, you can try Image.ActualHeight and Image.ActualWidth
If though it's not loaded from a server, it may still be loaded and processed asynchronously. If you use the same Debug.WriteLine
call after you know the image is visible, does that give the right results?
You have to wait for the ImageOpened event before you can get the Width/Heigth values. This is because the BitmapImage is being created asynchronously when loaded from a URI (even when the file is local). Only if you load it from a stream it gets created synchronously.
The other thing to be aware of is that BitmapImage.CreateOption is by default set to DelayCreation, which means it will only be created when the BitmapImage is assigned to an element in the live tree. You can set CreateOption = None to change that.
精彩评论