App Hub
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (6 posts)

A bug in SDK 7.1 : Can't get bitmap height/Width if using SetSource() and stream from media library

Last post 6/30/2011 9:14 AM by nishantcop. 5 replies.
  • 6/29/2011 5:05 AM

    A bug in SDK 7.1 : Can't get bitmap height/Width if using SetSource() and stream from media library

    Hi,

    I found a bug in WP7 Ver 7.1. try using following code:

                MediaLibrary library = new MediaLibrary();  
                Picture picture = library.Pictures[0];  
                BitmapImage bitmap = new BitmapImage();  
                bitmap.SetSource(picture.GetThumbnail());  
                int b = bitmap.PixelHeight;  
                b = bitmap.PixelWidth;  
                imageTest.Source = bitmap;  
                WriteableBitmap _bitmap = new WriteableBitmap(bitmap); 

    I am trying to extract an image from medialibrary then createing a bitmapImage and trying to set its source from a "Picture" extracted from medialibrary using a stream ImageStream from Picture.GetThumbnail() or Picture.GetImage().

    After doing this if we try to see bitmap's height or width it shows "0".

    My intention is to use it to create a WriteableBitmap but when I try it throws an exception (System.NullReferenceException "InvalidPointer").

    NOTE:- I have varified same code works fine with 7.0 wheares it throws errors in 7.1

    In short my intention was to copy images from MediaLibrary and store them in isolatedstorage and later read them back.
    But it seems because of this issue there is no way of doing it in 7.1

    Can any Microsoft guy confirm this ?

    Thanks and Regards
    Nishant Rana
  • 6/29/2011 10:10 AM In reply to

    Re: A bug in SDK 7.1 : Can't get bitmap height/Width if using SetSource() and stream from media library

    BitmapImage's loading is asynchronous. So when you try to use it right after the SetSource, it's not loaded yet. Try adding an handler for the BitmapImage's ‘ImageOpened' event, and create your WriteableBitmap there.
    There might be a way to make it synchronous, but I don't know how yet.
  • 6/30/2011 3:09 AM In reply to

    Re: A bug in SDK 7.1 : Can't get bitmap height/Width if using SetSource() and stream from media library

    Hi KooKiz,

    Thanks for your valuable information. As per your suggestion I tried this code:

        public partial class MainPage : PhoneApplicationPage  
        {  
            BitmapImage bitmap = new BitmapImage();  
            public MainPage()  
            {  
                InitializeComponent();  
                MediaLibrary library = new MediaLibrary();  
                Picture picture = library.Pictures[0];  
                bitmap.ImageOpened += new EventHandler<RoutedEventArgs>(imageTest_ImageOpened);  
                bitmap.SetSource(picture.GetThumbnail());  
                int b = bitmap.PixelHeight;  
                b = bitmap.PixelWidth;  
            }  
     
            void imageTest_ImageOpened(object sender, RoutedEventArgs e)  
            {  
                int b = bitmap.PixelHeight;  
                b = bitmap.PixelWidth;  
                WriteableBitmap _bitmap = new WriteableBitmap(bitmap);  
            }  
        } 
    And I tried to debug in WP7 and WP7.1 and found few interesting things:

    1. WP7.1 :
        a) bitmap.PixelHeight and Width is still set to "0" and ImageOpened event not yet fired within constructor.
        b) Can not create a WriteableBitmap using this bitmap in constructor.

        WP7.0 :
        a) bitmap.PixelHeight and Width is showing correct value my case "99" and ImageOpened event not yet fired within constructor.
        b) Can create a WriteableBitmap using this bitmap in constructor.

    2. WP7.1
        a) ImageOpened event fired width/Height is visible as "99" (in my case).
        b) Can create a WriteableBitmap using this bitmap within event.

        WP7.0 :
        a) ImageOpened event fired width/Height is visible as "99" (in my case).
        b) Can create a WriteableBitmap using this bitmap within event.

    So, YES, as you said we are able to see those properties after image is loaded.

    But the problem is if you see use case 1 where you can clearly see the difference and something which is missing in 7.1. The same thing works fine in 7.0 considering image is not being loaded yet.

    However, it may be a temperary workaround to create a writeableBitmap when image loads. But still this may not be sufficient enough if I want to save multiple images at some location. Since images are being loaded asynchronously so I won't be knowing what name to assign them properly :-(

    Thanks and Regards
    Nishant Rana
  • 6/30/2011 6:56 AM In reply to

    Re: A bug in SDK 7.1 : Can't get bitmap height/Width if using SetSource() and stream from media library

    Answer
    Reply Quote
    Hi Nishant,

    the behavior you are seeing in the 7.1 scenario comes from the fact that we have changed the default value for BitmapImage.CreateOptions to the new 'BackgroundCreation' mode. If you just set this property to 'None' before calling SetSource it will behave as in 7.0.

    We have realized that this change of default will lead to confusion and have already reverted to the old default in the meantime, after this public Beta build was created.

    Thanks,
    Stefan Wick - Microsoft Silverlight
  • 6/30/2011 7:10 AM In reply to

    Re: A bug in SDK 7.1 : Can't get bitmap height/Width if using SetSource() and stream from media library

    Hi KooKiz,

    could it be because of that:

    In Windows Phone OS 7.0, image decoding happened on the UI thread, causing the user interface responsiveness to suffer. In Windows Phone OS 7.1, image decoding happens on a background thread by default. Applications that are upgraded from Windows Phone Developer Tools 7.0 to Windows Phone SDK 7.1 Beta 2 are affected by this change. Applications that target Windows Phone OS 7.0 are not affected by this change.

    Caution: This change will be reverted in a future release.
     

    Found in Windows Phone SDK 7.1 Beta 2 Release Notes

  • 6/30/2011 9:14 AM In reply to

    Re: A bug in SDK 7.1 : Can't get bitmap height/Width if using SetSource() and stream from media library

    Thanks Stefan for your confirmation.

    They way you say it works fine.. :-)

    I have few question regarding this.

    1. Actually i was bewildered because when I was trying set BitmapCreate option, I found intellisense was showing (when we mouse over BitmapCreateOption.BackgroundCreation) in its documentation as "do not use". When we check for DelayCreation option it's documentation shows that it "is the default value". Hence, I thought DelayCreation should be the default option like WP7.0 and should work fine. While BackgroundCreation is for future release. So I guess that documentation need to udpate ?

    Stefan Wick:
    We have realized that this change of default will lead to confusion and have already reverted to the old default in the meantime, after this public Beta build was created.


    2. As you mention "this public Beta" is it public Beta 2 released yesterday  or first beta release?

    Thanks and Regards
    Nishant Rana

    PS: For others there is the link for detail info about off-thread bitmap creation (BackgroundCreation)

Page 1 of 1 (6 posts) Previous Discussion Next Discussion