0
\$\begingroup\$

I built a VB.NET WinForms application. I integrated ‘SkiaSharp’ into my program because Visual Basic's own graphics class is slower in terms of performance. Nevertheless, I have slight performance problems when the timer fires every 83 ms (≈ 12 times per second). I would like to improve this now before I get to the real point of the application – Fourier transform. Do you have a tip for me, please?

For the sake of better visibility, I draw each point a little thicker. Hence there are the three lines in the For-Each loop.

designer

Public NotInheritable Class Complex_Number
    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
Public Class FormMain
    Private ReadOnly List_of_complex_numbers_in_the_timespan As New List(Of Complex_Number)
    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
        Me.BackColor = Color.FromArgb(23, 23, 0) ' very dark yellow (almost black)
        For Each Bu As Button In Me.Controls.OfType(Of Button)
            Bu.BackColor = Color.FromArgb(255, 255, 0) ' yellow
        Next
        PictureBox1.BackColor = Color.Black
    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_complex_numbers_in_the_timespan.Clear()

        'Test filling 
        List_of_complex_numbers_in_the_timespan.Add(New Complex_Number(Rand.Next(-5, 5),
                                                                       Rand.Next(-5, 5)))
        List_of_complex_numbers_in_the_timespan.Add(New Complex_Number(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 Complex_Number In List_of_complex_numbers_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 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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.