Search code examples
c#visual-studioxamarinskiaskiasharp

Using SkiaSharp in Visual Studio


I'm researching the use of SkiaSharp for a future project, following the documentation currently available on GitHub:

https://developer.xamarin.com/guides/cross-platform/drawing/introduction/

I'm developing on Visual Studio 2013 on Windows 7. I tried using a Xamarin Android App project type but it requires a business licence for DllImportAttribute in SkiaSharp package.

I'm wondering if it's possible to select a C# Visual Studio project that will some how be able to display the SkiaSharp canvas, if so how would I do this?


Solution

  • The link on the comment is currently broken. Due to the fact that the folder "samples" might change path in the future again, whoever needs can start exploring from the https://github.com/mono/SkiaSharp page.

    More information about using SkiaSharp can be found on the API documentation online and in this article about drawing with skia sharp

    For who needs a practical quick start on how to use it here some examples:

    Getting an SKCanvas

    using (var surface = SKSurface.Create (width: 640, height: 480, SKColorType.N_32, SKAlphaType.Premul)) {
    SKCanvas myCanvas = surface.Canvas;
    
    // Your drawing code goes here.
    }
    

    Drawing Text

    // clear the canvas / fill with white
    canvas.DrawColor (SKColors.White);
    
    // set up drawing tools
    using (var paint = new SKPaint ()) {
      paint.TextSize = 64.0f;
      paint.IsAntialias = true;
      paint.Color = new SKColor (0x42, 0x81, 0xA4);
      paint.IsStroke = false;
    
      // draw the text
      canvas.DrawText ("Skia", 0.0f, 64.0f, paint);
    }
    

    Drawing Bitmaps

    Stream fileStream = File.OpenRead ("MyImage.png");
    
    // clear the canvas / fill with white
    canvas.DrawColor (SKColors.White);
    
    // decode the bitmap from the stream
    using (var stream = new SKManagedStream(fileStream))
    using (var bitmap = SKBitmap.Decode(stream))
    using (var paint = new SKPaint()) {
      canvas.DrawBitmap(bitmap, SKRect.Create(Width, Height), paint);
    }
    

    Drawing with Image Filters

    Stream fileStream = File.OpenRead ("MyImage.png"); // open a stream to an image file
    
    // clear the canvas / fill with white
    canvas.DrawColor (SKColors.White);
    
    // decode the bitmap from the stream
    using (var stream = new SKManagedStream(fileStream))
    using (var bitmap = SKBitmap.Decode(stream))
    using (var paint = new SKPaint()) {
      // create the image filter
      using (var filter = SKImageFilter.CreateBlur(5, 5)) {
        paint.ImageFilter = filter;
    
        // draw the bitmap through the filter
        canvas.DrawBitmap(bitmap, SKRect.Create(width, height), paint);
      }
    }