Using SkiaSharp for making graphs in WPF

abhishek.m 46 Reputation points
2020-09-07T06:27:02.533+00:00

Hi,
I have been trying to use SkiaSharp to make charts/graphs in WPF. Havent been able to find a proper sample implementation of Skia in WPF. Some of the available samples in GitHub seem to be complicated compared to other platforms. Can someone share with a sample simple implementation of Skia (using a canvas etc to draw something on the screen) in a WPF project ?

Thanks in Advance !

Developer technologies Windows Presentation Foundation
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,681 Reputation points
    2020-09-07T10:02:30.487+00:00

    This basic sample works fine for me (Windows 10 - 1909, VS 2019, .NET.4.7.2) : WPF SkiaSharpSample

    after retargeting to .NET.4.7.2, removing and installing latest versions for packages :
    SkiaSharp.Views.Desktop.Common
    SkiaSharp.Views.WPF

    0 comments No comments

  2. DaisyTian-1203 11,646 Reputation points
    2020-09-08T03:39:48.543+00:00

    I will show you the steps to create a simple demo for using SkiaSharp in WPF:

    Step 1: Create a .Net core WPF app and install SkiaSharp in its NuGet
    Step 2: Add the below code in MainWindow.xaml:

     <Grid>  
            <Grid.RowDefinitions>  
                <RowDefinition></RowDefinition>  
                <RowDefinition Height="Auto"></RowDefinition>  
            </Grid.RowDefinitions>  
            <Image x:Name="Image" Margin="10,10,10,10"></Image>  
            <Button Margin="10,10,10,10" Grid.Row="1" Content="use Skia " Click="Button_OnClick"></Button>  
        </Grid>  
    

    Step 3: Add the below code for MainWindow.xaml.cs:

    public partial class MainWindow : Window  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
            }  
      
            private void Button_OnClick(object sender, RoutedEventArgs e)  
            {  
                var writeableBitmap = CreateImage(1920, 1080);  
                UpdateImage(writeableBitmap);  
                Image.Source = writeableBitmap;  
            }  
      
            private WriteableBitmap CreateImage(int width, int height)  
            {  
                var writeableBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, BitmapPalettes.Halftone256Transparent);  
                return writeableBitmap;  
            }  
            private void UpdateImage(WriteableBitmap writeableBitmap)  
            {  
                int width = (int)writeableBitmap.Width,  
                    height = (int)writeableBitmap.Height;  
                writeableBitmap.Lock();  
                var skImageInfo = new SKImageInfo()  
                {  
                    Width = width,  
                    Height = height,  
                    ColorType = SKColorType.Bgra8888,  
                    AlphaType = SKAlphaType.Premul,  
                    ColorSpace = SKColorSpace.CreateSrgb()  
                };  
      
                using (var surface = SKSurface.Create(skImageInfo, writeableBitmap.BackBuffer))  
                {  
                    SKCanvas canvas = surface.Canvas;  
                    canvas.Clear(new SKColor(130, 130, 130));  
                    canvas.DrawText("SkiaSharp in Wpf!", 50, 200, new SKPaint() { Color = new SKColor(0, 0, 0), TextSize = 100 });  
                    canvas.DrawText("Using SkiaSharp for making graphs in WPF", new SKPoint(50, 500), new SKPaint(new SKFont(SKTypeface.FromFamilyName("Microsoft YaHei UI")))  
                    {  
                        Color = new SKColor(0, 0, 0),  
                        TextSize = 20  
                    });  
                }  
                writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));  
                writeableBitmap.Unlock();  
            }  
        }  
    

    Step 4: You will get the result picture after running the app:
    23184-capture.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.