在win8应用程序中,如何从canvascontrol(win2d)中获取绘图会话,或者,如何通过canvasControl_Draw函数在canvascontrol上绘制图像。
2 回答
You cannot - this is a key part of policy provided by CanvasControl.
This ensures that:
the drawingsession is created and closed at the appropriate time
drawing isn't attempted before resources have been created
handling device lost errors are handled
If you want to force a redraw you can use CanvasControl.Invalidate().
Alternatively, you may find that you want to render to an offscreen CanvasRenderTarget (that you can call CreateDrawingSession). Then use DrawImage in your CanvasControl_Draw to draw the render target to the control.
如果您的目标只是渲染到图像,您可以在不使用 CanvasControl.Draw 方法的情况下执行此操作。这是我的一个应用程序中的一些代码,它们呈现为图像并将其保存到文件中(PageRenderer 是我的渲染类):
public async Task GenerateThumbnailAsync(IRandomAccessStream stream, int width, int height, CanvasBitmapFileFormat imageType)
{
CanvasDevice device = CanvasDevice.GetSharedDevice();
PageRenderer renderer = new PageRenderer(device);
using (CanvasRenderTarget offscreen = new CanvasRenderTarget(device, width, height, 96))
{
using (CanvasDrawingSession ds = offscreen.CreateDrawingSession())
{
ds.Clear(Colors.Black);
renderer.DrawPage(ds);
}
await offscreen.SaveAsync(stream, imageType);
}
}