locked
why can't bitmapImage be frozen? RRS feed

  • Question

  • I'm unable to freeze a BitmapImage if its StreamSource is set to an instance of a Stream.  Example:

     

    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.StreamSource = stream; // an "instance" of type Stream
    bitmapImage.EndInit();
    
    bitmapImage.Freeze(); // "This freezable cannot be frozen" exception

    Please, enlighten me.  Thank you.

     


    Friday, November 13, 2009 7:55 PM

Answers

  • There are a few things that will prevent a BitmapImage from being frozen, but the thing you're most likely hitting here is that the image is still downloading.  You can freeze a BitmapImage using a Uri if you wait until DownloadComplete fires.  When using a Stream like you are here, I imagine that the web stream that you're using in your code sample isn't actually downloading the bits until they're requested by WPF when decode the image, so you can't freeze the BitmapImage until that happens.  When you convert the stream into a MemoryStream, that's probably causing the bits to download immediately, so when you use that as the source for the BitmapImage no more downloading needs to occur which is why you can Freeze() it right away.  If the work-around you discovered isn't adequate, try hooking DownloadComplete and Freeze()-ing the BitmapImage there.
    Thursday, November 19, 2009 10:58 PM

All replies

  • -- Kenneth Haugland
    Your referral.

    I'm familiar with the thread.  The author of the thread observes:

    bitmapImage1.StreamSource = memoryStream; <-- this can be frozen

    bitmapImage2.UriSource = new Uri(urlString); <-- this cannot be frozen

    The author of the thread concludes bitmapImage2 "couldn't be frozen because it was still downloading the bits."

    Perhaps he's right.  I don't know.  I've no idea of what new Uri(urlString) does behind the scenes.

    In a nutshell, my code is as follows:

    Stream myFunkyStream = webResponse.GetResponseStream();
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.StreamSource = myFunkyStream;
    bitmapImage.EndInit();
    
    bitmapImage.Freeze(); // this refuses to be frozen
    
    
    The underlying myFunkyStream is of the type System.Net.ConnectStream, which is undocumented.  However, I assume and expect myFunkyStream to be complete before it's pumped into the StreamSource of the bitmapImage.  Furthermore, if I convert myFunkyStream into MemoryStream, and pump the MemoryStream into StreamSource, the bitmapImage can be frozen merrily and readily.

    Please, enlighten me.

     


    Friday, November 13, 2009 11:49 PM
  • There are a few things that will prevent a BitmapImage from being frozen, but the thing you're most likely hitting here is that the image is still downloading.  You can freeze a BitmapImage using a Uri if you wait until DownloadComplete fires.  When using a Stream like you are here, I imagine that the web stream that you're using in your code sample isn't actually downloading the bits until they're requested by WPF when decode the image, so you can't freeze the BitmapImage until that happens.  When you convert the stream into a MemoryStream, that's probably causing the bits to download immediately, so when you use that as the source for the BitmapImage no more downloading needs to occur which is why you can Freeze() it right away.  If the work-around you discovered isn't adequate, try hooking DownloadComplete and Freeze()-ing the BitmapImage there.
    Thursday, November 19, 2009 10:58 PM