BitmapImage Constuctor(Uri)无法在Windows Phone 7上运行

时间:2012-06-01 02:00:00

标签: c# uri windows-phone-7.1 bitmapimage

BitmapImage tempBitmap = new BitmapImage(onlineImageLocation);

相当简单的代码。 OnlineImageLocation指的是http://dantonybrown.com/brownsoft/SweepyCleaner.png

但是在构造之后BitMap图像不包含弹出的字段。甚至PixelWidth和PixelHeight都是0。

有什么想法吗?

丹尼

1 个答案:

答案 0 :(得分:2)

比如这样?

    BitmapImage bmpImage = new BitmapImage(new Uri("http://dantonybrown.com/brownsoft/SweepyCleaner.png"));
    MessageBox.Show(bmpImage.PixelWidth.ToString());

这很有道理。图像按需加载,并在后台加载。这里有多个选项:

  1. 将BitmapImage指定给Image控件。您可以在ImageLoaded事件发生后访问属性:

        BitmapImage bmpImage = new BitmapImage(new Uri("http://dantonybrown.com/brownsoft/SweepyCleaner.png"));
        bmpImage.ImageOpened += (sender, args) => Dispatcher.BeginInvoke(() => MessageBox.Show(
            bmpImage.PixelWidth.ToString(CultureInfo.InvariantCulture)));
        imageCtrl.Source = bmpImage;
    
  2. 使用CreateOptions.None加载BitmapImage。这仍然会在后台加载图像,但您不必在开始加载之前将图像分配给控件:

        BitmapImage bmpImage = new BitmapImage(new Uri("http://dantonybrown.com/brownsoft/SweepyCleaner.png"))
                                   {CreateOptions = BitmapCreateOptions.None};
        bmpImage.ImageOpened += (sender, args) => Dispatcher.BeginInvoke(() => MessageBox.Show(
            bmpImage.PixelWidth.ToString(CultureInfo.InvariantCulture)));