如何在silverlight中获取图像的属性?

发布于 2024-08-12 02:57:00 字数 1164 浏览 2 评论 0原文

我正在将图像加载到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离去的眼神 2024-08-19 02:57:00

首先,这有点令人困惑:-

    imageBrush.ImageSource = img;

    InitializeComponent();

除非发生非常不寻常的事情,否则 imageBrush 对象将为 null,直到 InitializeComponent 运行之后。

至于你的问题,猜测你在 MainPage_Loaded 中加载 3D 模型。那么问题是他的发生与位图的到达异步。因此,在 LoadedImageOpened 事件都发生之前,您不想实际加载 3D 模型。请注意,假设 ImageOpened 总是最后发生是很危险的。

我能想到的最简单的解决方案是将 MainPage_Loaded 事件中的所有现有代码移动到 ImageOpened,然后将获取图像的代码移动到 MainPage_Loaded< /代码>。这会序列化序列,当执行 ImageOpened 时,您可以保证页面已加载。

这不是最复杂的解决方案,也没有利用 SL 异步特性的优势。但是,它应该可以帮助您继续前进,并且您可以评估并行操作页面加载和位图下载是否有任何好处。

First off this is a little confusing:-

    imageBrush.ImageSource = img;

    InitializeComponent();

Unless you have something quite unusual happening the imageBrush object will be null until after the InitializeComponent 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 both Loaded and ImageOpened events have both occured. Note it would be dangerous to assume that ImageOpened would always happen last.

The simplest solution I can think of would be to move all your existing code in the MainPage_Loaded event to ImageOpened then move the code that fetches the image to the MainPage_Loaded. This serialises the sequence, when execution of ImageOpened 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文