SKCanvas save image

MudMud USMember ✭✭
edited October 2017 in SkiaSharp

I'd like to reduce battery usage in my SkiaSharp-based app.

My app draws an expensive but static background, then draws moving elements over it. I want to render the background once to a bitmap then blit it onto future frames.

I don't see any way of saving the pixels of a SKCanvas at that level of the API, so it looks like I need to use an SKSurface.

My thought is something like:

SKSurface background = null;

myMainDrawRoutine(SKCanvas canvas) {

    if (background == null) {
        background = SKSurface.Create(???);
        // draw background into background.Canvas

    // blit pre-rendered background into canvas
    background.Draw(canvas, 0, 0, null);

    // now draw foreground elements

Is that the right approach? How best to create the SKCanvas so it's compatible with the main canvas? What parameters ensure that drawing to the background surface results in a raster image and not queued drawing commands or something else?



  • mattleibowmattleibow ZAXamarin Team Xamurai

    That is a good way.

    The paint event args has both the surface as well as the image info, which you can use to create the second surface. In addition, it doesn't really matter exactly what the second surface is, it will convert during the draw - it may also be good just to use the default SKImageInfo values.

    Creating a new surface via SKSurface.Create with the SKImageInfo parameter overloads will create a raster image.

    You may also want to keep an eye on the canvas size, if this is a mobile device, a rotation should recreate the surface with a new size.

Sign In or Register to comment.