如何在silverlight中获取图像的属性?
我正在将图像加载到我的 silverlight 应用程序中。该图像将作为纹理贴图适合 3D 模型。我需要获取图像属性。为此,我使用 ImageOpened 事件,如下所示:
public MainPage()
{
BitmapImage img = new BitmapImage(new Uri("imagens/textura.jpg", UriKind.Relative));
img.ImageOpened += new EventHandler<RoutedEventArgs>(img_ImageOpened);
img.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(img_ImageFailed);
imageBrush.ImageSource = img;
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
this.MouseLeftButtonUp += new MouseButtonEventHandler(MainPage_MouseLeftButtonUp);
(...)
然后:
private void img_ImageOpened(object sender, RoutedEventArgs e)
{
BitmapImage i = sender as BitmapImage;
ImgSize.Width = i.PixelWidth;
ImgSize.Height = i.PixelHeight;
MessageBox.Show("LOADED IMAGE SIZE\n W:" + ImgSize.Width.ToString() + " H:" + ImgSize.Height.ToString());
}
消息框显示加载图片的正确值。但这是在场景加载后运行的,因此大小始终是默认的(0,0)......我不知道如何解决这个问题。我已经运行了调试器,我注意到场景和模型已渲染,并且图片宽度和高度为零。此后,事件被触发......我不明白。
预先感谢,
何塞
I am loading an image to my silverlight application. This image will fit in a 3d model as a texture map. I need to fetch image attributes. For that I am using ImageOpened event, like this:
public MainPage()
{
BitmapImage img = new BitmapImage(new Uri("imagens/textura.jpg", UriKind.Relative));
img.ImageOpened += new EventHandler<RoutedEventArgs>(img_ImageOpened);
img.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(img_ImageFailed);
imageBrush.ImageSource = img;
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
this.MouseLeftButtonUp += new MouseButtonEventHandler(MainPage_MouseLeftButtonUp);
(...)
and then:
private void img_ImageOpened(object sender, RoutedEventArgs e)
{
BitmapImage i = sender as BitmapImage;
ImgSize.Width = i.PixelWidth;
ImgSize.Height = i.PixelHeight;
MessageBox.Show("LOADED IMAGE SIZE\n W:" + ImgSize.Width.ToString() + " H:" + ImgSize.Height.ToString());
}
The messagebox is showing the correct values for the loaded picture. But this is running after the scene is loaded, so the size is always default (0,0) ... I don't know how to fix this. I've runned the debugger, i've noticed that the scene and the model is rendered and the picture width and height is zero. After this, the event is fired ... I can't figure it out.
Thanks in advance,
Jose'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,这有点令人困惑:-
除非发生非常不寻常的事情,否则
imageBrush
对象将为 null,直到InitializeComponent
运行之后。至于你的问题,猜测你在
MainPage_Loaded
中加载 3D 模型。那么问题是他的发生与位图的到达异步。因此,在Loaded
和ImageOpened
事件都发生之前,您不想实际加载 3D 模型。请注意,假设ImageOpened
总是最后发生是很危险的。我能想到的最简单的解决方案是将
MainPage_Loaded
事件中的所有现有代码移动到ImageOpened
,然后将获取图像的代码移动到MainPage_Loaded< /代码>。这会序列化序列,当执行
ImageOpened
时,您可以保证页面已加载。这不是最复杂的解决方案,也没有利用 SL 异步特性的优势。但是,它应该可以帮助您继续前进,并且您可以评估并行操作页面加载和位图下载是否有任何好处。
First off this is a little confusing:-
Unless you have something quite unusual happening the
imageBrush
object will be null until after theInitializeComponent
has run.As to your question at a guess you load the 3D model in
MainPage_Loaded
. The problem then is that his happens asynchronously with the arrival of the bitmap. Hence you do not want to actually load the 3D Model until bothLoaded
andImageOpened
events have both occured. Note it would be dangerous to assume thatImageOpened
would always happen last.The simplest solution I can think of would be to move all your existing code in the
MainPage_Loaded
event toImageOpened
then move the code that fetches the image to theMainPage_Loaded
. This serialises the sequence, when execution ofImageOpened
you are guaranteed that the page has loaded.Not the most sophisticated solution and doesn't make use of benefits of the asynchronous nature of SL. However it should get you going and you can assess whether there is any benefit in having Page loading and bitmap downloading operating in parallel.