在运行时获取图像的宽度和高度 - WP8

时间:2014-05-05 09:39:45

标签: c# image silverlight windows-phone-8 windows-phone

我正在开发Windows Phone 8应用程序。

我有一张图片路径 - /Data/Images/image1.png。我可以在屏幕上显示此图像,但我想在渲染之前更改图像的宽度和高度。

这是我在webbrowser control

中显示图片的方式
webbrowser.Append("<img src=""+path+ width=\"250\" height=\"250\" style=\"vertical-align:middle\" alt=\"\"></img>"/>"

这里我将宽度和高度设置为250x250,但我想更改高度和宽度,因为有些图像效果不佳。

1 个答案:

答案 0 :(得分:5)

如果您想获得图片的大小,则需要将其加载到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
}