msbg
msbg

Reputation: 4972

BitmapImage.ImageOpened Not Firing

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?

Upvotes: 4

Views: 3186

Answers (4)

IUnknown
IUnknown

Reputation: 659

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.

Upvotes: 1

Thomas Uttendorfer
Thomas Uttendorfer

Reputation: 415

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

Upvotes: 0

msbg
msbg

Reputation: 4972

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();
}

Upvotes: 5

NSGaga
NSGaga

Reputation: 14312

(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

Upvotes: 1

Related Questions