none
CachedBitmap returned by BitmapSource.Create()? RRS feed

  • Question

  • I am doing the following:

    BitmapSource myNewBitmapSource = BitmapSource.Create(...bunch of parameters...);

    BitmapImage myNewImage = (BitmapImage)myNewBitmapSource;

    However, the second line throws the error saying unable to cast CachedBitmap to BitmapImage. I'm not sure why the Create function is returning a CachedBitmap instead of a BitmapImage or even what that is. Can anyone tell me what I'm doing wrong?

    Thanks!

     

    -JL

     

    Tuesday, October 17, 2006 5:25 PM

Answers

  • Hi, Justin. You're not doing anything wrong, it's just that BitmapSource.Create() just doesn't return a BitmapImage . Now, BitmapImage is a kind of BitmapSource, just not the kind that BitmapSource creates.

    Do you need to be using a BitmapImage? From a consumer ("consumer" as in producer/consumer) standpoint, any type of BitmapSource should provide pretty much the functionality as a BitmapImage. The only major difference is how they are constructed. The major value add that BitmapImage provides is that it's easy to construct them in XAML. If you find that you really need a BitmapImage, you can construct one using the following syntax:

    BitmapImage bitmap = new BitmapImage();
    bitmap.BeginInit();
    bitmap.UriSource = new Uri("foo.jpg", UriKind.RelativeOrAbsolute);
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    // ...
    bitmap.EndInit();

    Tuesday, October 17, 2006 5:58 PM

All replies

  • Hi, Justin. You're not doing anything wrong, it's just that BitmapSource.Create() just doesn't return a BitmapImage . Now, BitmapImage is a kind of BitmapSource, just not the kind that BitmapSource creates.

    Do you need to be using a BitmapImage? From a consumer ("consumer" as in producer/consumer) standpoint, any type of BitmapSource should provide pretty much the functionality as a BitmapImage. The only major difference is how they are constructed. The major value add that BitmapImage provides is that it's easy to construct them in XAML. If you find that you really need a BitmapImage, you can construct one using the following syntax:

    BitmapImage bitmap = new BitmapImage();
    bitmap.BeginInit();
    bitmap.UriSource = new Uri("foo.jpg", UriKind.RelativeOrAbsolute);
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    // ...
    bitmap.EndInit();

    Tuesday, October 17, 2006 5:58 PM
  • Thanks for the info!

    After looking over my code I determined that you were right--I didn't really need a BitmapImage after all; the BitmapSource worked fine. It's still all a bit confusing, but I'll get it sooner or later.

     

    JL

     

    Tuesday, October 17, 2006 11:16 PM
  • BitmapSource.Create(from byte array) creates a CachedBitmap and it can't be converted to anything like BitmapSource or BitmapFrame. Visual Studio claims it returns a BitmapSource but it most certainly doesn't. Looking at the source code for BitmapSource.Create confirms this.

    So you were right to ask and weren't doing anything wrong.

    Anyone please prove me wrong as it would be nice to have an image library with multipage tiff support that will generate an image from a byte array.

    Sunday, June 1, 2014 9:18 PM