防止 Win2D Animated Canvas 清除

Prevent Win2D Animated Canvas from clearing

我想保留 CanvasAnimatedControl 中最后绘制的帧以在其上绘制。
是否有内置功能可以执行此操作,还是我必须使用双缓冲。

我想用 win2d 实现 this

如前所述here

Controls are always cleared automatically by Win2D when a drawing session is created. CanvasRenderTargets are not. This way, apps have the ability to make incremental changes to CanvasRenderTargets, and avoid redrawing an entire scene every time.

CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget offscreen = new CanvasRenderTarget(device, width, height, 96);
using (CanvasDrawingSession ds = offscreen.CreateDrawingSession())
{
    //ds.Clear(Colors.Black); // no clear and you are good to go.
    ds.DrawRectangle(100, 200, 5, 6, Colors.Red);
}

Note that there is a method call to Clear. Without this, the bitmap will be initialized with undefined content.

To draw a CanvasRenderTarget to another drawing session, simply use DrawImage(ICanvasImage) or one of its overloads. For example:

void canvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
    args.DrawingSession.DrawImage(offscreen, 23, 34);
}