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?

3
  • 1
    I'm guessing - it's never used - thus it never loads or opens - just a thought but makes sense I think
    – NSGaga
    Mar 25, 2013 at 22:07
  • You were right. Turns out that BitmapImages do not load until they need to.
    – msbg
    Mar 25, 2013 at 22:17
  • do you want me to post an answer as well - if it made you do the right thing - so you can up/close
    – NSGaga
    Mar 25, 2013 at 22:25

4 Answers 4

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();
}
4
  • 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? Apr 18, 2013 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, 2013 at 3: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, 2013 at 18:25
  • @VitaliiVasylenko looking back months later I realize it should say BitmapCreateOptions.DelayCreation is the default. fixing.
    – msbg
    Oct 4, 2013 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

1
  • Thanks, didn't exactly solve the problem but certainly got me thinking in the right direction.
    – msbg
    Mar 25, 2013 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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.