0
BitmapImage tempBitmap = new BitmapImage(onlineImageLocation);

Fairly simple code. OnlineImageLocation refers to http://dantonybrown.com/brownsoft/SweepyCleaner.png

but after construction BitMap image contains no poplated fields. Even PixelWidth and PixelHeight are 0.

Any ideas?

Danny

1 Answers1

2

For instance like this?

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

That makes perfect sense. The image is loaded on demand, and on the background. You have multiple options here:

  1. Assign the BitmapImage to an Image control. You can access the properties after the ImageLoaded event occured:

        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. Load the BitmapImage with CreateOptions.None. This will still load the image in the background, but you don't have to assign the image to a control before it starts loading:

        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)));
    
Erwin B.
  • 196
  • 4