问题描述
我试图做一个超级简单的事情:把图像的大小。
所以我创建的BitmapImage
,但我得到0,当我尝试访问 PixelWidth
和 PixelHeight
。
我怎样才能实现这个目标吗?
编辑:(加入样品code)
我只是在做:
VAR基本URI =新的URI(MS-APPX:///);
VAR的BitmapImage =新的BitmapImage(新的URI(基本URI,资产/ Logo.png));
MyImage.Source = BitmapImage的;
的Debug.WriteLine(宽:+ bitmapImage.PixelWidth +身高+ bitmapImage.PixelHeight);
在控制台中,我得到:
宽度:0身高:0
在你想设置的来源,为您的,你通常需要编写一个事件处理程序,将在执行 ImageOpened
。
还记得 ImageOpened
仅触发如果图像被下载和去codeD(即使用 Image.Source
)。
VAR的BitmapImage =新的BitmapImage(URI);
bitmapImage.ImageOpened + =(发件人,E)=>
{
的Debug.WriteLine(宽度:{0},身高:{1},
bitmapImage.PixelWidth,bitmapImage.PixelHeight);
};
image.Source = BitmapImage的;
I'm trying to do a super simple thing : get the size of an image.
So I create my BitmapImage
but I'm getting 0 when I try to access the PixelWidth
and PixelHeight
.
How can I accomplish that please?
EDIT: (added sample code)
I'm just doing:
var baseUri = new Uri("ms-appx:///");
var bitmapImage = new BitmapImage(new Uri(baseUri, "Assets/Logo.png"));
MyImage.Source = bitmapImage;
Debug.WriteLine("Width: " + bitmapImage.PixelWidth + " Height: " + bitmapImage.PixelHeight);
In the console, I get:
Width: 0 Height: 0
When you want to use the image properties after setting the source for your BitmapImage
, you normally have to write an event handler that will execute on ImageOpened
.
Also remember that ImageOpened
only fires if the image is downloaded and decoded (i.e. using Image.Source
).
var bitmapImage = new BitmapImage(uri);
bitmapImage.ImageOpened += (sender, e) =>
{
Debug.WriteLine("Width: {0}, Height: {1}",
bitmapImage.PixelWidth, bitmapImage.PixelHeight);
};
image.Source = bitmapImage;
这篇关于BitmapImage的返回0 PixelWidth和PixelHeight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!