简体   繁体   中英

Get width and height of image at run time - WP8

I am working on Windows Phone 8 app.

I have a path to image - /Data/Images/image1.png . I am able to display this image on screen, but i want to change the width and height of the image before its rendered.

This is how i am displaying the image in webbrowser control

webbrowser.Append("<img src=""+path+ width=\"250\" height=\"250\" style=\"vertical-align:middle\" alt=\"\"></img>"/>"

Here i am setting width and height as 250x250 but i want to change that height and width as some images are not looking good.

If you want the get the size of an image, you need to load it in a BitmapImage :

int width = 0;
int height = 0;
using (var stream = Application.GetResourceStream(new Uri("Assets/test.jpg", UriKind.Relative)).Stream)
{
    var bmpi = new BitmapImage();
    bmpi.SetSource(stream);
    bmpi.CreateOptions = BitmapCreateOptions.None;
    width = bmpi.PixelWidth;
    height = bmpi.PixelHeight;
    bmpi = null; // Avoids memory leaks
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM