WP7 set Image component height/width to BitmapImage height/width
I am trying to set an Image component source to a BitmapImage:
However, the
Uri newUri = new Uri("images/" + filename, UriKind.Relative);
Image newImage = new Image();
BitmapImage bmp = new BitmapImage(newUri);
newImage.Source = bmp;
However, if the image is smaller than the screen is is scaled to fill it.
I have tried setting the width and height as follows:
newImage.Height = bmp.PixelHeight;
newImage.Width = bmp.PixelWidth;
but the BitmapImage retur开发者_开发知识库ns zero values for the width and height. I presume this is because it hasn't "loaded" yet?
How do I force this to give me the right values or atleast force the Image component to "wrap" only fit itself to the size of the bitmap.
All my images are pixel perfect sizes for the screen.
Thanks
The Bitmap doesn't know it's PixelHeight and PixelWidth until it's actually been loaded, which is why you're getting zero. However, rather than trying to manually set the Width and Height, simply set the Stretch property to None and the image will always be it's original size.
NOTE: If you're using pixel perfect images as part of your layout, make sure it copes with the orientation change to landscape :)
精彩评论