I have just run Visual Studio's own code analysis with the "All Microsoft rules" setting. This warning came up:
"CA2000: Call System.IDisposable.Dispose in the "FormMain.processing()" method for the "New SKPaint()" object before all references to it are outside the valid scope."
But: I'm using a using block. There is no need to write .Dispose(), right? What's happening?
I have to mention that the automatic garbage collection clears the memory after about 26–30 seconds. I can see that in the diagnostic tools. The memory consumption increases by then up to approximately 800 MB. However, I never saw in the SkiaSharp documentation that you have to write .Dispose yourself.
This is my original code. For a minimally reproducible example, see below.
#Disable Warning CA2007 ' Aufruf von "ConfigureAwait" für erwarteten Task erwägen
Public NotInheritable Class FormMain
Private ReadOnly List_of_ComplexNumbers_in_the_timespan As New List(Of ComplexNumber)
Private Shared displayedBitmap As Drawing.Bitmap
Private Shared ReadOnly Rand As New Random
Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
Timer1.Start()
Timer_GC.Start()
End Sub
Private Sub ButtonStop_Click(sender As Object, e As EventArgs) Handles ButtonStop.Click
Timer1.Stop()
Timer_GC.Stop()
End Sub
Private Async Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Await Task.Run(Sub() processing())
End Sub
Private Sub processing()
List_of_ComplexNumbers_in_the_timespan.Clear()
'Test filling
List_of_ComplexNumbers_in_the_timespan.Add(New ComplexNumber(Rand.Next(-5, 5),
Rand.Next(-5, 5)))
List_of_ComplexNumbers_in_the_timespan.Add(New ComplexNumber(Rand.Next(-5, 5),
Rand.Next(-5, 5)))
displayedBitmap = Nothing
Using surface As SkiaSharp.SKSurface = SkiaSharp.SKSurface.Create(New SkiaSharp.SKImageInfo(PictureBox1.Size.Width,
PictureBox1.Size.Height))
Using canvas As SkiaSharp.SKCanvas = surface.Canvas
canvas.Translate(PictureBox1.Size.Width \ 2,
PictureBox1.Size.Height \ 2)
Using Magenta As New SkiaSharp.SKPaint With {
.IsAntialias = True,
.Color = New SkiaSharp.SKColor(255, 0, 64),
.Style = SkiaSharp.SKPaintStyle.Fill
}
'point of origin
canvas.DrawLine(-50.0F, 0.0F, 50.0F, 0.0F, Magenta)
canvas.DrawLine(0.0F, -50.0F, 0.0F, 50.0F, Magenta)
For Each Instance As ComplexNumber In List_of_ComplexNumbers_in_the_timespan
Dim SKPoints0 As New SkiaSharp.SKPoint(Instance.x * 50.0F + 2.0F, -(Instance.y * 50.0F + 2.0F))
Dim SKPoints1 As New SkiaSharp.SKPoint(Instance.x * 50.0F - 2.0F, -(Instance.y * 50.0F - 2.0F))
Dim SKPoints2 As New SkiaSharp.SKPoint(Instance.x * 50.0F, -(Instance.y * 50.0F))
canvas.DrawPoints(SkiaSharp.SKPointMode.Lines,
{SKPoints0, SKPoints1, SKPoints2},
Magenta)
Next
End Using
End Using
'–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
' Get the data into displayedBitmap because the PictureBox is only accepting an usual System.Drawing.Bitmap.
'–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Using image As SkiaSharp.SKImage = surface.Snapshot()
Using data As SkiaSharp.SKData = image.Encode(SkiaSharp.SKEncodedImageFormat.Png, 100)
Using mStream As New IO.MemoryStream(data.ToArray())
displayedBitmap = New Drawing.Bitmap(mStream, False)
End Using
End Using
End Using
End Using
PictureBox1.Image = Nothing
PictureBox1.Image = displayedBitmap
End Sub
Private Sub Timer_GC_Tick(sender As Object, e As EventArgs) Handles Timer_GC.Tick
GC.Collect()
End Sub
End Class
#Enable Warning CA2007 ' Aufruf von "ConfigureAwait" für erwarteten Task erwägen
Public NotInheritable Class ComplexNumber
Public Property x As Single
Public Property y As Single
Public Sub New(ByVal x As Single, ByVal y As Single)
Me.x = x
Me.y = y
End Sub
End Class
minimal reproducible example You need 2 Buttons, 1 Timer, and 1 PictureBox. You also need to install SkiaSharp.

Public NotInheritable Class Form1
Private Shared displayedBitmap As Drawing.Bitmap
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer1.Stop()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
displayedBitmap = Nothing
Using surface As SkiaSharp.SKSurface = SkiaSharp.SKSurface.Create(New SkiaSharp.SKImageInfo(PictureBox1.Size.Width,
PictureBox1.Size.Height))
Using canvas As SkiaSharp.SKCanvas = surface.Canvas
canvas.Translate(PictureBox1.Size.Width \ 2,
PictureBox1.Size.Height \ 2)
Using Magenta As New SkiaSharp.SKPaint With {
.IsAntialias = True,
.Color = New SkiaSharp.SKColor(255, 0, 64),
.Style = SkiaSharp.SKPaintStyle.Fill
}
'point of origin
canvas.DrawLine(-50.0F, 0.0F, 50.0F, 0.0F, Magenta)
canvas.DrawLine(0.0F, -50.0F, 0.0F, 50.0F, Magenta)
End Using
End Using
'–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
' Get the data into displayedBitmap because the PictureBox is only accepting an usual System.Drawing.Bitmap.
'–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Using image As SkiaSharp.SKImage = surface.Snapshot()
Using data As SkiaSharp.SKData = image.Encode(SkiaSharp.SKEncodedImageFormat.Png, 100)
Using mStream As New IO.MemoryStream(data.ToArray())
displayedBitmap = New Drawing.Bitmap(mStream, False)
End Using
End Using
End Using
End Using
PictureBox1.Image = Nothing
PictureBox1.Image = displayedBitmap
End Sub
End Class
The same warning appears.
