SkiaSharp C# Drawing Research

SkiaSharp is a .NET wrapper for Google's Skia graphics library that can be used to draw 2D graphics across mobile, server, and desktop platforms. SkiaSharp can be used with OpenGL for hardware accelerated rendering. SkiaSharp was originally developed by Mono, but is now maintained by Microsoft and available under the MIT License.
Use SkiaSharp to achieve cross-platform use, and support the use on windows, Linux, Anroid, IOS, and WebAssembly. The source code itself is C++ code, which supports the development of multiple languages ​​after packaging.

Basic use of SkiaCharp

  1. Create a WinForm program
  2. Use nuget to install SkiaSharp, SkiaSharp.Views.WindowsForms, SkiaSharp.Extended library, and its version number should correspond to prevent dependency errors. (WPF also has a similar library)
  3. Add skControl to the interface in the toolbox

image.png

using SkiaSharp;

namespace WinFormsApp1
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }

        private void skControl1_PaintSurface(object sender, SkiaSharp.Views.Desktop.SKPaintSurfaceEventArgs e)
        {
    
    
           var canvas= e.Surface.Canvas;
            SKPaint paint = new SKPaint
            {
    
    
                Style = SKPaintStyle.Stroke,
                Color = SKColors.Red,
                StrokeWidth=2,
                //设置抗锯齿
                IsAntialias = true,
            };
            //设置虚实线;
            float[] dash_param = {
    
     5F, 3F };
            SKPathEffect dashes = SKPathEffect.CreateDash(dash_param, 0);
            paint.PathEffect = dashes;
            

            SKPaint fill_paint = new SKPaint
            {
    
    
                Style = SKPaintStyle.Fill,
                Color = new SKColor(255,0,0,125),
                StrokeWidth = 10

            };
            canvas.Clear(new SKColor(100, 100, 100, 125));
            canvas.DrawLine(0, 0, 200, 200, paint);
            
            canvas.DrawRect(0, 0, 500, 500, fill_paint);

            SKPaint text_paint = new SKPaint
            {
    
    
                FakeBoldText = true,
                TextSize = 50,
                Color = SKColors.Green,
                TextAlign = SKTextAlign.Center,
                //宋体,不然不支持中文绘制;
                Typeface = SKTypeface.FromFamilyName("SimSun")

            };
            canvas.Save();
            //逆时针旋转45度绘制
            canvas.RotateDegrees(-45, 250, 250);
            canvas.DrawText("我是中国人", new SKPoint(250, 250), text_paint);
            canvas.Restore();
        }
    }
}

show

image.png

Draw Canvas content to image, PDF, SVG and other devices

//创建基于PDF、SVG、PNG的绘图文件.
public SKCanvas createFile(string file_name, int width, int height, StoreType store_type)
{
    
    
    var wStream = new SKFileWStream(file_name);

    if (store_type == StoreType.StoreSvg)
    {
    
    
        return SKSvgCanvas.Create(SKRect.Create(width, height), wStream);
    }
    else if (store_type == StoreType.StorePDF)
    {
    
    
         var _pdf_doc = SKDocument.CreatePdf(wStream);
        _pdf_doc.BeginPage(width, height);
        return pdf_doc;
    }
    else if (store_type == StoreType.StorePng)
    {
    
    
       return SKSurface.CreateNull(width, height);
    }
    return NULL;
}
//将内容绘制到图片上,创建基于图片的canvas即可;
public SKCanvas GetCanvas(SKBitmap bitmap)
{
    
    
    return  new SKCanvas(bitmap);
}

//将数据绘制到内存上,并直接进行到各自的类型文件中
public SKCanvas createStream(MemoryStream memStream, int width, int height, XDICanvas.StoreType store_type)
{
    
    

    SKManagedWStream wstream = new SKManagedWStream(memStream);

    if (store_type == StoreType.StoreSvg)
    {
    
    
        return SKSvgCanvas.Create(SKRect.Create(width, height), wstream);
    }
    else if (store_type == StoreType.StorePDF)
    {
    
    
        var _pdf_doc = SKDocument.CreatePdf(wstream);
        var file_canvas = _pdf_doc.BeginPage(width, height);
         return file_canvas;
    }
    return true;
}

Read Svg diagrams via Skia

By default, SkiaSharp does not provide the reading function of SVG, which is also an extension function in its C++ bottom layer, and uses Svg.Skiathe library to process Svg images.

public void DrawSvg(SKCanvas canvas,string svg_data)  //svg的字符串
{
    
    
		SvgDocument svgDocument;
#if NETFOR60
        svgDocument=SvgExtensions.FromSvg(svg_data);
#else    
            //低版本的使用方式;
        svgDocument = SvgDocument.FromSvg<SvgDocument>(svg_data);
#endif
            
        var rect = new XDRect(0, 0, svgDocument.Width.Value, svgDocument.Height.Value);
        SKSvg svg = new SKSvg();
       
        var picture = svg.FromSvgDocument(svgDocument);

        canvas.DrawPicture(picture):
             
}

How to use Skia to solve the problem of drawing text automatic line break

By default, the Skia library does not directly provide an automatic line-wrapping solution for text, and it is relatively difficult to write it yourself. It is found that there is already an open source library SkiaTextRendererthat can solve the problem of word wrapping. This library provides the function of automatically processing Chinese and English words. The following is the result of its test example.

image.png

About the mutual conversion of objects such as SKBitmap and Bitmap

One of the things that makes C# very easy to use is that it provides many existing solutions. As long as you want to find it, you will always find a suitable solution. SKia's conversions in objects such as SKBitmap, SKImage and System.Drawing have provided some existing libraries, SkiaSharp.Views.Desktop.Commonand the libraries provide related conversion solutions

image.png

Guess you like

Origin blog.csdn.net/qq_33377547/article/details/126691992