4

I am trying to download an image and have an event fire when it finishes. I use this:

BitmapImage btest = new BitmapImage(new Uri("http://www.google.com/images/srpr/logo4w.png"));
btest.ImageOpened += btest_ImageOpened;

void btest_ImageOpened(object sender, RoutedEventArgs e)
{
    throw new NotImplementedException();
}

However, the ImageOpened event will not fire. If I set an Image Control's source to the BitmapImage using:

image.Source = btest;

It does fire. Why doesn't the ImageOpened event fire unless the BitmapImage sender is set as an Image's source?

joce
  • 9,624
  • 19
  • 56
  • 74
msbg
  • 4,852
  • 11
  • 44
  • 73

4 Answers4

5

I figured this out myself. By default, a BitmapImage will not be initialized until necessary. The default value of a BitmapImage's CreateOptions is BitmapCreateOptions.DelayCreation. All that is needed to fix this is to set CreateOptions to BitmapCreateOptions.None.

My working code is:

BitmapImage btest = new BitmapImage(new Uri("http://www.google.com/images/srpr/logo4w.png"));
btest.CreateOptions = BitmapCreateOptions.None;
btest.ImageOpened += btest_ImageOpened;

void btest_ImageOpened(object sender, RoutedEventArgs e)
{
    throw new NotImplementedException();
}
msbg
  • 4,852
  • 11
  • 44
  • 73
  • 1
    **The default value of a BitmapImage's CreateOptions is BitmapCreateOptions.None. All you need to do to fix this is set CreateOptions to BitmapCreateOptions.None.** Hm, so CreateOptions should be set to None? – Vitalii Vasylenko Apr 18 '13 at 17:03
  • I couldn't get this to work - perhaps because btest.CreateOptions = BitmapCreateOptions.None is already the default value. Is there another solution? – Cam Jul 17 '13 at 03:33
  • 1
    It isn't the default according to the documentation. BitmapCreateOptions.DelayCreation is the default. Changing this to none does not seem to work either. – Paul Wade Sep 11 '13 at 18:25
  • @VitaliiVasylenko looking back months later I realize it should say BitmapCreateOptions.DelayCreation is the default. fixing. – msbg Oct 04 '13 at 23:06
1

(I'll just post this based on our discussion - as it helped OP get to the right solution)

I'm guessing - it's never used - thus it never loads or opens - just a thought but makes sense I think

NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • Thanks, didn't exactly solve the problem but certainly got me thinking in the right direction. – msbg Mar 25 '13 at 22:35
1

My two cents, maybe it helps someone else... Placing an image control on the page and wiring any event and source in xaml works fine, and the events fire. However when I load a bitmap image in code and set the image control's source, the events for the image control do not fire although the image is loaded fine. I tried all the bitmap options stated above and none of them seemed to have worked. I ended up handling the ImageOpened event of the bitmap image and NOT of the image control, which fired off the event. Take note though that at this stage the image control has not loaded the image completely, so you will have to reference the bitmap image for details and not the image control.

IUnknown
  • 559
  • 6
  • 12
0

In my Windows phone 8.0 silverlight application I get the event ImageOpened when I set the creation options to BitmapCreateOptions.BackgroundCreation.

I don't get it when setting to

BitmapCreateOptions.DelayCreation (which is the default)

or BitmapCreateOptions.None