Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight 4 BitmapImage bug : ImageOpened not invoked after SetSource()

This seems like a serious bug :

        private void LayoutRoot_Drop(object sender, DragEventArgs e)
        {
            if ((e.Data != null) && (e.Data.GetDataPresent(DataFormats.FileDrop)))
            {
                FileInfo[] files = (FileInfo[])e.Data.GetData(DataFormats.FileDrop);

                using (FileStream fileStream = files[0].OpenRead())
                {
                    //Code reaching this point.
                    BitmapImage bmpImg = new BitmapImage();
                    bmpImg.ImageOpened += new EventHandler<RoutedEventArgs>(bmpImg_ImageOpened);
                    bmpImg.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(bmpImg_ImageFailed);
                    try
                    {
                        bmpImg.SetSource(fileStream);                        
                    }
                    catch
                    {
                        //Code dosen't reach here.                        
                    }
                }
            }          
        }

        void bmpImg_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            //Code dosen't reach here. 
        }

        void bmpImg_ImageOpened(object sender, RoutedEventArgs e)
        {
            //Code dosen't reach here. 
        }

I am experiencing a very strange behivour. Running this code on my computer, it works - when you drag a JPG on the LayoutRoot I can break inside bmpImg_ImageOpened().

But on a different machine it won't work - when dragging a JPG, I can break in the drop event but after SetSource() nothing happens : no exceptions are thrown, and non of the callbacks are invoked.

I tried it on another machine and it also didn't work.

edit: On all of the machines, when adding an Image class and setting it's Source property to the bitmapImage, the image is shown fine. so I guess it's an issue with the callbacks. This is not enough because I still need those events.

I am banging my head here, what could it be ?

like image 417
Yaron Levi Avatar asked Feb 22 '26 00:02

Yaron Levi


2 Answers

This is simply how Silverlight has always behaved. ImageOpened only fires if the image is downloaded and decoded (i.e. using Source). It does not fire when using SetSource. If you need access to the dimensions after loading your image either use WriteableBitmap for the PixelWidth and PixelHeight properties (instead of BitmapImage) or do something like:

img.Source = bmpImg;
Dispatcher.BeginInvoke(() =>
{
   FakeImageOpened(); // Do logic in here
});
like image 174
David Avatar answered Feb 24 '26 14:02

David


You have to set

bitmapImage.CreateOptions = BitmapCreateOptions.None;

Then the ImageOpened event is fired. This is because the default Options are CreateDelayed

Greetings

Christian http://www.wpftutorial.net

like image 21
Christian Moser Avatar answered Feb 24 '26 14:02

Christian Moser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!