Pictures on every system
The picture classes of earlier lessons (Bitmap, Graphics) are a Windows feature. SkiaSharp, a package from nuget.org, draws, saves and reads pictures on Windows, Linux and macOS. This lesson draws a small picture, looks at its pixels, saves it as PNG, and reads the file back - and shows how a package with a native library is brought to Ring.
What you will learn#
- why
System.Drawingis not the answer on every system, and what SkiaSharp offers instead, - how a package with a native library is published and found,
- a bitmap, a canvas, a paint and colors,
- reading pixels, and saving and reading a PNG file,
Why another picture library#
The lessons Images and An image filter lab use Bitmap and Graphics of Windows. In current versions of .NET these classes work on Windows only, so a program that makes pictures on a Linux server needs another library. SkiaSharp is one: the drawing engine of Chrome and Android, with a .NET interface. It makes the same picture on every system.
A drawing engine is written in C++, so SkiaSharp is two things: a managed DLL (SkiaSharp.dll) and a native library for each system (libSkiaSharp.dll for Windows, libSkiaSharp.so for Linux). The package of the managed part includes the native libraries for Windows and macOS. The one for Linux is a separate package, so the project file names both:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<AssemblyName>Skia</AssemblyName>
<DebugType>none</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.88.9" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.9" />
</ItemGroup>
</Project>dotnet publish puts every native library in a folder of its own system, Skia/libs/runtimes/win-x64/native, .../linux-x64/native and so on. The bridge looks in the folder of the running system when .NET asks for a native library that is not found next to the DLL, so the program does not copy anything.
The steps#
They are the steps of the lessons A NuGet package, A QR code and Excel files: a project file, dotnet publish, wrapassembly.ring for the namespace SkiaSharp, then dnLoad and dnImportFrom.
The program#
# pictures that work on every system - SkiaSharp draws, saves and reads pictures on Windows, Linux and macOS (System.Drawing is Windows only)
# (the project is in the folder Skia: Skia.csproj names SkiaSharp and its Linux library, and Empty.cs is the one source file a project needs)
load "dotnet.ring"
using { "System.IO" }
cHere = currentdir()
cDll = cHere + "/Skia/libs/SkiaSharp.dll"
cClasses = cHere + "/Skia/classes"
# STEP 1. Get the package (nuget.org the first time) with dotnet publish: SkiaSharp.dll, and its native library in Skia/libs/runtimes
if not fexists(cDll)
? "getting the package..."
system("dotnet publish Skia -c Release -o Skia/libs -nologo -v q > nul")
ok
if not fexists(cDll)
? "The package could not be restored. It is downloaded from nuget.org the first time, so the computer needs the internet once."
bye
ok
# STEP 2. Make Ring classes for the namespace SkiaSharp
chdir("../../../tools/dotnettools")
system(exefilename() + " wrapassembly.ring " + cDll + " " + cClasses + " SkiaSharp > nul")
chdir(cHere)
# STEP 3. Load the assembly and the classes
dnLoad(cDll)
dnImportFrom(cClasses + "/","SkiaSharp")
func main
# 1. A bitmap is a grid of pixels, and a canvas is what draws on it. Colors are values of the type SKColor: Parse reads "#rrggbb".
oBitmap = new SKBitmap().@New([ :width = 200, :height = 100 ])
oCanvas = new SKCanvas(oBitmap)
oBlue = new SKColor().Parse("#2060c0")
oYellow = new SKColor().Parse("#ffcc00")
oWhite = new SKColor().Parse("#ffffff")
oCanvas.Clear(oBlue)
# 2. A paint says how to draw: its color, and whether the edges are smooth
oPaint = new SKPaint
oPaint.Color = oYellow
oPaint.IsAntialias = true
oCanvas.DrawCircle([ :cx = 150, :cy = 50, :radius = 30, :paint = oPaint ])
oPaint.Color = oWhite
oPaint.TextSize = 28
oCanvas.DrawText([ :text = "Ring", :x = 15, :y = 62, :paint = oPaint ])
? "1. the picture is " + oBitmap.Width + " x " + oBitmap.Height + " pixels"
# 3. Look at pixels: a pixel is an SKColor with Red, Green, Blue and Alpha from 0 to 255
oCorner = oBitmap.GetPixel(0,0)
oCenter = oBitmap.GetPixel(150,50)
? "2. corner (0,0): " + oCorner.Red + ", " + oCorner.Green + ", " + oCorner.Blue
? " center of the circle (150,50): " + oCenter.Red + ", " + oCenter.Green + ", " + oCenter.Blue
# 4. Save as PNG: Encode gives the data of the picture, ToArray its bytes (a Ring text), and File writes them
cFile = new Path().Combine([new Path().GetTempPath(),"ringdotnet-picture.png"])
oImage = new SKImage().FromBitmap(oBitmap)
cPng = oImage.Encode(NULL).ToArray()
new File().WriteAllBytes(cFile,cPng)
? "3. a PNG file was written: " + (substr(cPng,2,3) = "PNG") + ", " + new File().Exists(cFile)
# 5. Read the file back: Decode makes a bitmap again, and the pixels are the same
oBack = new SKBitmap().Decode(cFile)
oPixel = oBack.GetPixel(150,50)
? "4. read back: " + oBack.Width + " x " + oBack.Height + ", center " + oPixel.Red + ", " + oPixel.Green + ", " + oPixel.Blue
new File().Delete(cFile)1. the picture is 200 x 100 pixels
2. corner (0,0): 32, 96, 192
center of the circle (150,50): 255, 204, 0
3. a PNG file was written: 1, 1
4. read back: 200 x 100, center 255, 204, 0How it works#
Bitmap, canvas, paint#
- An
SKBitmapis the picture: a grid of pixels.@New([ :width = 200, :height = 100 ])makes an empty one (a constructor chosen by the names of its parameters). - An
SKCanvasis what draws on a bitmap:new SKCanvas(oBitmap). Its methods areClear,DrawCircle,DrawRect,DrawTextand more, and each of the ones with several versions takes a list of the names of its parameters. - An
SKPaintsays how to draw: itsColor,IsAntialias(smooth edges),TextSize. The same paint can draw many things, and it can be changed between them: the program draws the circle in yellow and the text in white with one paint.
Colors#
A color is a value of the type SKColor, with Red, Green, Blue and Alpha (the opacity) from 0 to 255. new SKColor().Parse("#2060c0") reads the usual web notation. Colors are values, so the program makes them once (oBlue, oYellow, oWhite) and uses them where it needs them.
Pixels#
oBitmap.GetPixel(150,50) is the color of the pixel at column 150 and row 50 (the top left corner is 0,0). The program checks two pixels: the corner is the blue of Clear, and the center of the circle is the yellow of the paint.
Saving and reading#
SKImage is a finished picture. new SKImage().FromBitmap(oBitmap) makes one, and Encode(NULL) gives the data of a PNG file (the default format). ToArray() returns the bytes, and a byte[] that .NET returns is a Ring text, so File.WriteAllBytes(cFile, cPng) writes the file. The second to fourth bytes are the letters PNG, which the program uses as a test. new SKBitmap().Decode(cFile) reads a file again, and the pixels are the same.
For another format, give the format and the quality: Encode([ :format = "Jpeg", :quality = 80 ]).
Many pixels are many calls#
Every GetPixel or SetPixel crosses the bridge, like the SetPixel of the image lesson, and a picture has thousands of them. Use the drawing methods for shapes and text. For a filter over a whole picture, read the pixels once with a method that returns many at once (the lessons An image filter lab and Core concepts explain why).
Which system#
SkiaSharp works on Windows, Linux and macOS. The program here was run on Windows and on Linux (x64) and printed the same lines. macOS and arm64 were not tested, although the package has native libraries for them (the Linux one needs the extra package of the project file).
What you learned#
System.Drawingis Windows-only, and SkiaSharp draws on every system,- a package with a native library is published with its
runtimesfolder, and the bridge finds the library of the running system, - bitmap, canvas, paint and colors; pixels; PNG and JPEG.
Exercises#
- Draw a rectangle:
oCanvas.DrawRect([ :x = 10, :y = 10, :w = 60, :h = 30, :paint = oPaint ]), in another color. - Move the circle, change its size, and print the color of a pixel on its edge.
- Save the picture as JPEG with
Encode([ :format = "Jpeg", :quality = 80 ])and compare the sizes of the two files. - Write a function
drawFlag(cColor1, cColor2, cColor3)that draws three stripes and returns the PNG bytes.