Search code examples
c#wpfbitmapimagerotatetransform

TransformedBitmap to BitmapImage


I have an Observable Collection of BitmapImage type in which I store some images, this collection is bound to the UI.

Now the problem is, I want to make changes to these images, for example rotate. Now in order to rotate, I have to make a TransformedBitmap out of the BitmapImages, this is not the problem.

When I am done, I want to place them back into the collection, here comes the problem.

Cannot implicitly convert type System.Windows.Media.Imaging.TransformedBitmap to System.Windows.Media.Imaging.BitmapImage

I have searched long and wide, and cannot find a solution for this problem.

The question: How can I convert a TransformedBitmap back into a BitmapImage?


Solution

  • For binding the Source property of Image controls in your UI it is sufficient to use BitmapSource (or even ImageSource) as element type of the ObservableCollection.

    You could then add any derived bitmap type:

    public ObservableCollection<BitmapSource> Bitmaps { get; set; }
    ...
    Bitmaps.Add(new BitmapImage(new Uri(...)));
    Bitmaps.Add(new TransformedBitmap(Bitmaps[0], new RotateTransform(90)));