8

I have a question. I have a skiasharp canvas with a few bitmaps. Now I want to export the canvas with the bitmaps to an Image, but I have no idea how to do that, or if that is possible.

Can someone help me?

  • 3
    Show your code so that when someone answers they can replicate your code and include the answer to make your life easier! – Kevin Hernandez Dec 18 '19 at 14:26
  • 1
    There is no code to show, because I am asking if something is possibe! –  Dec 18 '19 at 21:35

2 Answers2

15

You are probably looking to take a Snapshot of the Surface.

using (var image = surface.Snapshot())
using (var data = image.Encode(SKEncodedImageFormat.Png, 80))
using (var stream = File.OpenWrite(Path.Combine(FolderPath, "1.png")))
{
    // save the data to a stream
    data.SaveTo(stream);
}

This is very similar to MShah's answer, but just from a snapshot of the surface instead, given that surface is a reference to your Skia Surface.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
1
  1. To convert the bitmap to image:

    using (var image = SKImage.FromBitmap(yourBitmap))
    using (var data = image.Encode(SKEncodedImageFormat.Png, 80))
    {
    // save the data to a stream
    using (var stream = File.OpenWrite(Path.Combine(FolderPath, "1.png")))
          {
             data.SaveTo(stream);
           }
    }
    

    Edit:

  2. To convert canvas to png:

    SKImage mainCanvasImage = surface.Snapshot();
    SKBitmap TempTIFbitmap1 = SKBitmap.Decode(mainCanvasImage.Encode())
    

    Use the above code from point 1 to save this.

hope this will resolve your issue.

MShah
  • 1,247
  • 9
  • 14
  • No, I want to convert the whole canvas to an image. The canvas contains multiple bitmaps! –  Dec 18 '19 at 13:39
  • @Vreesie If you have multiple bitmaps draw on single canvas then you can capture that surface snapshot. Please find my edit. – MShah Dec 18 '19 at 13:56