Goofy
Goofy

Reputation: 6128

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.

Upvotes: 0

Views: 815

Answers (1)

Olivier Payen
Olivier Payen

Reputation: 15268

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
}

Upvotes: 5

Related Questions