1

I am working of this answer, trying to convert an ImageSource to a BitmapImage. I have an Image control on a WPF canvas, the source of which is a bitmap, and I want to extract the source property of this control into a variable (which will be an ImageSource), and then cast this as a BitmapImage.

My code is below:

ImageSource source = MyImage.Source;
BitmapImage image = (BitmapImage) source;

It is basically the same code as in the linked answer, but I am getting the following runtime error on the second line (where I am trying to cast):

Unable to cast object of type 'System.Windows.Media.Imaging.BitmapFrameDecode' to type 'System.Windows.Media.Imaging.BitmapImage'.

The logical answer I guess is that this simply can't be done, but the answer linked is accepted, has a favorable comment and four upvotes which is frustrating. I am using Visual Studio 2012 with .Net 4.5, I have tried to compile against 4.0, 3.5 and 3.0, and am seeing the same issue.

I have also tried Ashura's suggestion here and the result is simply null (as in whenever I use the as keyword instead of casting the value of the resulting variable is null).

In my XAML, the source property of my Image control is a *.bmp file, I am ultimately trying to get this to a WriteableBitmap, but I need to have a BitmapImage to pass into the WriteableBitmap's constructor.

So my question is, what am I doing wrong? How do I convert from an ImageSource to a BitmapImage?

Community
  • 1
  • 1
JMK
  • 27,273
  • 52
  • 163
  • 280
  • 1
    Check this: http://stackoverflow.com/a/7507946/754438 – Renatas M. Dec 19 '12 at 10:54
  • Well, it's not and final type may be different according to what you loaded into your source (don't assume a specific type if not absolutely necessary, I see it as an implementation detail). Edit your answer to say what you need to do with it... – Adriano Repetti Dec 19 '12 at 10:55
  • 2
    BitmapFrameDecode is inherited from BitmapFrame -> BitmapSource -> ImageSource... BitmapImage is inherited from BitmapSource, so the 2 classes aren't compatible and this explains your error message. – Jeroen Landheer Dec 19 '12 at 10:58
  • @JMK Why do you need to convert to BitmapImage? Maybe it would be sufficient to cast to [BitmapSource](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.aspx), which is a common base class of BitmapImage and BitmapFrame. Which BitmapImage properties do you really need? – Clemens Dec 19 '12 at 11:08

1 Answers1

1

Thank you to the commenters, you helped me understand the problem.

I ended up taking the Source property out of my XAML, and set the below in the Window Loaded event to ensure that the Source property of my image control was indeed a BitmapImage:

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
    var image = new BitmapImage(new Uri("sample.bmp", UriKind.Relative));
    MyImage.Source = image;
}

If you do this in the XAML instead, the Source property ends up being a BitmapFrameDecode object by default.

Mike Yang
  • 2,581
  • 3
  • 24
  • 27
JMK
  • 27,273
  • 52
  • 163
  • 280