how to get a drawingsession from a canvascontrol (

2019-06-02 23:14发布

In a win8 app,how to get a drawingsession from a canvascontrol (win2d),or ,how to draw image on canvascontrol out of the function:canvasControl_Draw.

标签: win2d
2条回答
闹够了就滚
2楼-- · 2019-06-02 23:45

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.

查看更多
闹够了就滚
3楼-- · 2019-06-03 00:04

If your goal is just to render to an image, you can do this without being in the CanvasControl.Draw method. Here is some code from one of my apps that renders to an image and saves it to a file (PageRenderer is my class that does the rendering):

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);
    }
}
查看更多
登录 后发表回答