How to bitblit from RenderTargetBitmap to WriteableBitmap?
Asked Answered
D

1

7

I'm rendering dozens of visuals to the RenderTargetBitmap. Each is rendered in it's own Rect. What I want to do is to copy one of these Rect areas rendered from RenderTargetBitmap instance into the same area of the WriteableBitmap...Fast copy rect pixels or smth. like that.

So, is there a way to copy rect from RenderTargetBitmap to WriteableBitmap in a fast way?

Daynadays answered 4/2, 2011 at 17:55 Comment(0)
D
4

Solved by copying the whole RenderTargetBitmap to WriteableBitmap like this:

 protected override void OnRender(DrawingContext drawingContext)
 {
   if (ActualWidth == 0 || ActualHeight == 0) return;
   // Create destination bitmap
   wb = new WriteableBitmap((int) ActualWidth, (int) ActualHeight, 96, 96, PixelFormats.Pbgra32, null);
   wb.Lock();
   rtb = new RenderTargetBitmap(wb.PixelWidth, wb.PixelHeight, wb.DpiX, wb.DpiY, PixelFormats.Pbgra32);
   foreach (MyVisual visual in visuals)
   {
     visual.Render(rtb);
   }

   rtb.CopyPixels(new Int32Rect(0,0, rtb.PixelWidth, rtb.PixelHeight), 
   wb.BackBuffer,
   wb.BackBufferStride * wb.PixelHeight,  wb.BackBufferStride);

   wb.AddDirtyRect(new Int32Rect(0, 0, (int)ActualWidth, (int)ActualHeight));
   wb.Unlock();

   drawingContext.DrawImage(wb, new Rect(0, 0, ActualWidth, ActualHeight));
}
Daynadays answered 8/2, 2011 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.