编辑
2025-11-11
C#
00

Detailed Guide to Drawing Circles and Ellipses with SkiaSharp

SkiaSharp is a powerful 2D graphics library that can be used to draw various shapes. This article will provide a detailed introduction on how to use SkiaSharp to draw circles and ellipses in WinForms applications.

Environment Setup

First, you need to install SkiaSharp related packages through NuGet Package Manager:

  • SkiaSharp
  • SkiaSharp.Views
  • SkiaSharp.Views.WindowsForms

Basic Knowledge

In SkiaSharp:

  • Use SKCanvas.DrawCircle() method to draw circles
  • Use SKCanvas.DrawOval() method to draw ellipses
  • Use SKPaint class to set drawing styles (color, line width, etc.)
编辑
2025-11-11
C#
00

SkiaSharp 是一个强大的 2D 图形库,可以用来绘制各种图形。本文将详细介绍如何使用 SkiaSharp 在 WinForms 应用程序中绘制圆形和椭圆。

环境准备

首先需要通过 NuGet 包管理器安装 SkiaSharp 相关包:

  • SkiaSharp
  • SkiaSharp.Views
  • SkiaSharp.Views.WindowsForms

基础知识

在 SkiaSharp 中:

  • 使用 SKCanvas.DrawCircle() 方法绘制圆形
  • 使用 SKCanvas.DrawOval() 方法绘制椭圆
  • 使用 SKPaint 类设置绘制样式(颜色、线宽等)
编辑
2025-11-11
C#
00

SkiaSharp is the .NET version of Google's Skia graphics engine, providing powerful 2D graphics processing capabilities. This article will provide a detailed introduction on how to use SkiaSharp to draw various rectangles in WinForm applications.

Environment Setup

First, you need to install the following packages through NuGet Package Manager:

  • SkiaSharp
  • SkiaSharp.Views
  • SkiaSharp.Views.WindowsForms

Basic Example

Creating Basic Form

C#
using SkiaSharp; using SkiaSharp.Views.Desktop; namespace AppRectangle { public partial class Form1 : Form { private SKControl skControl; public Form1() { InitializeComponent(); // Create SKControl control skControl = new SKControl(); skControl.Dock = DockStyle.Fill; skControl.PaintSurface += OnPaintSurface; this.Controls.Add(skControl); } // Paint event handler private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e) { // Get drawing canvas SKCanvas canvas = e.Surface.Canvas; // Clear canvas (using white background) canvas.Clear(SKColors.White); // Add drawing code here DrawRectangles(canvas); } private void DrawRectangles(SKCanvas canvas) { // Create paint brush using (var paint = new SKPaint()) { // Enable anti-aliasing paint.IsAntialias = true; // 1. Draw basic rectangle paint.Color = SKColors.Blue; // Set color to blue paint.Style = SKPaintStyle.Fill; // Set fill style canvas.DrawRect(50, 50, 200, 100, paint); // 2. Draw stroked rectangle paint.Color = SKColors.Red; // Set color to red paint.Style = SKPaintStyle.Stroke; // Set to stroke style paint.StrokeWidth = 3; // Set stroke width canvas.DrawRect(50, 200, 200, 100, paint); // 3. Draw rounded rectangle paint.Color = SKColors.Green; // Set color to green paint.Style = SKPaintStyle.Fill; // Set fill style canvas.DrawRoundRect(50, 350, 200, 100, 20, 20, paint); } } } }

image.png

编辑
2025-11-11
C#
00

SkiaSharp 是 Google's Skia 图形引擎的 .NET 版本,提供了强大的 2D 图形处理能力。本文将详细介绍如何在 WinForm 应用程序中使用 SkiaSharp 绘制各种矩形。

环境准备

首先需要通过 NuGet 包管理器安装以下包:

  • SkiaSharp
  • SkiaSharp.Views
  • SkiaSharp.Views.WindowsForms

基础示例

创建基础窗体

C#
using SkiaSharp; using SkiaSharp.Views.Desktop; namespace AppRectangle { public partial class Form1 : Form { private SKControl skControl; public Form1() { InitializeComponent(); // 创建 SKControl 控件 skControl = new SKControl(); skControl.Dock = DockStyle.Fill; skControl.PaintSurface += OnPaintSurface; this.Controls.Add(skControl); } // 绘制事件处理 private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e) { // 获取绘图画布 SKCanvas canvas = e.Surface.Canvas; // 清空画布(使用白色背景) canvas.Clear(SKColors.White); // 在这里添加绘制代码 DrawRectangles(canvas); } private void DrawRectangles(SKCanvas canvas) { // 创建画笔 using (var paint = new SKPaint()) { // 启用防锯齿 paint.IsAntialias = true; // 1. 绘制基础矩形 paint.Color = SKColors.Blue; // 设置颜色为蓝色 paint.Style = SKPaintStyle.Fill; // 设置填充样式 canvas.DrawRect(50, 50, 200, 100, paint); // 2. 绘制描边矩形 paint.Color = SKColors.Red; // 设置颜色为红色 paint.Style = SKPaintStyle.Stroke; // 设置为描边样式 paint.StrokeWidth = 3; // 设置描边宽度 canvas.DrawRect(50, 200, 200, 100, paint); // 3. 绘制圆角矩形 paint.Color = SKColors.Green; // 设置颜色为绿色 paint.Style = SKPaintStyle.Fill; // 设置填充样式 canvas.DrawRoundRect(50, 350, 200, 100, 20, 20, paint); } } } }

image.png

编辑
2025-11-11
C#
00

SkiaSharp is a powerful cross-platform 2D graphics drawing library derived from Google's Skia graphics library. This article will explore in depth how to use SkiaSharp to draw various types of lines.

Basic Line Drawing

C#
override protected void OnPaint(PaintEventArgs e) { var info=new SKImageInfo(ClientSize.Width, ClientSize.Height); using (var surface = SKSurface.Create(info)) { var canvas = surface.Canvas; canvas.Clear(SKColors.White); // Create Paint object using (var paint = new SKPaint()) { // Set line color to blue paint.Color = SKColors.Blue; // Set line width paint.StrokeWidth = 5; // Set drawing mode to stroke paint.Style = SKPaintStyle.Stroke; // Draw a line from point (50, 50) to point (200, 200) canvas.DrawLine(50, 50, 200, 200, paint); } using(var view=new SKGLControl()) { // Generate snapshot image from the drawing surface var image = surface.Snapshot(); // Generate SKBitmap from image var bitmap = SKBitmap.FromImage(image); // Convert SKBitmap to GDI+ Bitmap and draw to Graphics object of PaintEventArgs e.Graphics.DrawImage(bitmap.ToBitmap(), 0, 0); } } }

image.png