how to get a drawingsession from a canvascontrol (win2d)

爷,独闯天下 提交于 2019-12-10 11:38:43

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/32526653/how-to-get-a-drawingsession-from-a-canvascontrol-win2d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!