如何从 RenderTargetBitmap 位传输到 WriteableBitmap?

发布于 2024-10-16 02:10:56 字数 217 浏览 2 评论 0原文

我正在将数十个视觉效果渲染到 RenderTargetBitmap。每个都在它自己的矩形中渲染。 我想要做的是将 RenderTargetBitmap 实例渲染的这些矩形区域之一复制到 WriteableBitmap 的同一区域...快速复制矩形像素或 smth。像那样。

那么,有没有一种方法可以快速地将 rect 从 RenderTargetBitmap 复制到 WriteableBitmap 呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2024-10-23 02:10:56

通过将整个 RenderTargetBitmap 复制到 WriteableBitmap 来解决,如下所示:

 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));
}

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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文