0

How can I convert a RenderTargetBitmap to BitmapImage in C# XAML, Windows 8.1?

I tried

// rendered is the RenderTargetBitmap 
BitmapImage img = new BitmapImage();
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
await randomAccessStream.WriteAsync(await rendered.GetPixelsAsync());
randomAccessStream.Seek(0); 
await img.SetSourceAsync(randomAccessStream);

But it always gives error at

img.SetSourceAsync(randomAccessStream);

There are many ways in WPF but in WinRT? How can I do that ?

Thanks a lot!

Jannik
  • 2,310
  • 6
  • 32
  • 61
Bassem Wissa
  • 2,947
  • 1
  • 20
  • 20

2 Answers2

2

this is the one that worked Sharing render to bitmap image in windows phone 8.1

turned out that i just can't fill the stream directly using

stream.WriteAsync(byteArray.AsBuffer());

you have to use bitmap encoder , final working code :

InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
        var buffer = await rendered.GetPixelsAsync();
      //  await stream.ReadAsync(buffer, (uint)buffer.Length, InputStreamOptions.None);
        BitmapImage img = new BitmapImage();
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
        encoder.SetPixelData(
            BitmapPixelFormat.Bgra8,
            BitmapAlphaMode.Straight,
            (uint)rendered.PixelWidth,
            (uint)rendered.PixelHeight,
            DisplayInformation.GetForCurrentView().LogicalDpi,
            DisplayInformation.GetForCurrentView().LogicalDpi,
            buffer.ToArray());
        await encoder.FlushAsync();
        await img.SetSourceAsync(stream);
        preview.Source = img;
Community
  • 1
  • 1
Bassem Wissa
  • 2,947
  • 1
  • 20
  • 20
1

Have you tried this :

  var bitmap = new  RenderTargetBitmap();

  await bitmap.RenderAsync(elementToRender);

  image.Source = bitmap;

Reference : http://social.technet.microsoft.com/wiki/contents/articles/20648.using-the-rendertargetbitmap-in-windows-store-apps-with-xaml-and-c.aspx

UPDATE :

Another Refs.. may help :

UPDATE 2 :

Try this one :

private async Task<BitmapImage> ByteArrayToBitmapImage(byte[] byteArray)
    {
        var bitmapImage = new BitmapImage();

        var stream = new InMemoryRandomAccessStream();
        await stream.WriteAsync(byteArray.AsBuffer());
        stream.Seek(0);

        bitmapImage.SetSource(stream);
        return bitmapImage;
    }

Ref : C# Windows 8 Store (Metro, WinRT) Byte array to BitmapImage

Community
  • 1
  • 1
Nasser AlNasser
  • 1,725
  • 2
  • 11
  • 12
  • dude thanks, yep but i Just want the randomAccessStream and it always crashes or any type of streams :D I just need the bytes or stream that i can use later to create another bitmapimages – Bassem Wissa Oct 05 '15 at 12:05
  • 1
    What is the error message or the exception type ? any screenshots ? .. Answer updated – Nasser AlNasser Oct 05 '15 at 12:09
  • 1
    dude the "Sharing render to bitmap image in windows phone 8.1" post is the one that worked, turned out that i have to fill the stream using bitmap encoder not directly from the stream.WriteAsync(buffer); thanks a lot for your help :) – Bassem Wissa Oct 05 '15 at 12:40