13

I have a RenderTargetBitmap, I need to convert it to BitmapImage. Please check the code below.

 RenderTargetBitmap bitMap = getRenderTargetBitmap();
 Image image = new Image();// This is a Image
 image.Source = bitMap;

In the above code I have used Image.Now I need to use a BitmapImage. How can I do this?

 RenderTargetBitmap bitMap = getRenderTargetBitmap();
 BitmapImage image = new BitmapImage();// This is a BitmapImage
 // how to set bitMap as source of BitmapImage ?
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
sharmila
  • 1,493
  • 5
  • 23
  • 42
  • Georges,I need to convert the RenderTargetBitmap to BitmapImage. – sharmila Dec 21 '12 at 09:30
  • Possible duplicate: [RenderTargetBitmap to BitmapImage in WPF](http://stackoverflow.com/questions/3361579/rendertargetbitmap-to-bitmapimage-in-wpf) – khellang Dec 21 '12 at 09:31
  • 2
    something like that : http://msdn.microsoft.com/en-us/library/aa969819.aspx – GeorgesD Dec 21 '12 at 09:35
  • Please explain why exactly you need to have a BitmapImage? RenderTargetBitmap and BitmapImage have a common base class [BitmapSource](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.aspx), which provides all the basic bitmap properties. – Clemens Dec 21 '12 at 09:39
  • MSDN: "BitmapImage primarily exists to support Extensible Application Markup Language (XAML) syntax and introduces additional properties for bitmap loading that are not defined by BitmapSource.". Do you really need that? – Clemens Dec 21 '12 at 09:43
  • The questioner doesn't mention it, but you cannot set RenderTargetBitmap.Rotation but you can set BitmapImage.Rotation. – Jesse Chisholm Jan 11 '13 at 21:11

2 Answers2

23

Although it doesn't seem to be necessary to convert a RenderTargetBitmap into a BitmapImage, you could easily encode the RenderTargetBitmap into a MemoryStream and decode the BitmapImage from that stream.

There are several BitmapEncoders in WPF, the sample code below uses a PngBitmapEncoder.

var renderTargetBitmap = getRenderTargetBitmap();
var bitmapImage = new BitmapImage();
var bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

using (var stream = new MemoryStream())
{
    bitmapEncoder.Save(stream);
    stream.Seek(0, SeekOrigin.Begin);

    bitmapImage.BeginInit();
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.StreamSource = stream;
    bitmapImage.EndInit();
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
-1
private async void Button_Click(object sender, RoutedEventArgs e)
{
    RenderTargetBitmap bitMap = new RenderTargetBitmap();
    await bitMap.RenderAsync(grid);
    Image image = new Image();// This is a Image
    image.Source = bitMap;
    image.Height = 150;
    image.Width = 100;

    grid.Children.Add(image);
}

This looks as a simpler solution.

YakovL
  • 7,557
  • 12
  • 62
  • 102
  • 1
    Please answer in English and ask questions as separate questions (or via comments when you have enough reputation) – YakovL Sep 10 '16 at 23:10
  • This isn't even a solution at all (despite a simpler one), because it does not create a BitmapImage, as was asked for in the question. – Clemens Jan 09 '17 at 12:04