LED-Display zu laggy

  • VB.NET

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von ThePlexian.

    LED-Display zu laggy

    Hallo Community !

    Ich habe vor ein LED-Display zu coden, um Klassensysteme kennenzulernen.
    Ich habe das ganze logischerweise als neue .dll erstellt, und darin eine Class für UserControls.
    Die Aufmachung funktioniert soweit, die (De-)Aktivierung eines Punktes auch, Texteingabe ist in Arbeit, aber:
    Display ist tierisch langsam / laggy, es ist mir kaum möglich das Display auf der Form richtig einzustellen.

    Hier mein OnPaint-Sub, aber ich weiß nicht wie ich das vereinfachen soll, weil das ja alles neu gezeichnet werden muss :/ :

    VB.NET-Quellcode

    1. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    2. Dim graph As Graphics = e.Graphics
    3. 'Background
    4. graph.FillRectangle(New SolidBrush(my_backcolor), 0, 0, Me.Width, Me.Height) 'Dim my_backcolor As Color = Color.Black
    5. 'Circles
    6. For x As Integer = 1 To my_status.GetUpperBound(0) 'Dim my_status(2000, 1000) As Boolean --> Status einer einzelnen Lampe
    7. For y As Integer = 1 To my_status.GetUpperBound(1) ' True = An - False = Aus
    8. Dim sb As SolidBrush
    9. If my_status(x, y) = False Then
    10. sb = New SolidBrush(Color.FromArgb(230, my_inactivecolor)) 'Dim my_inactivecolor As Color = Color.FromARGB(14,55,14)
    11. Else
    12. sb = New SolidBrush(Color.FromArgb(230, my_activecolor)) 'Dim my_activecolor As Color = Color.Green
    13. End If
    14. Dim ptx As Integer = my_circlemargin + (my_circlediameter + my_circlemargin) * (x - 1) 'my_circlemagin = Abstand zwischen den "LEDs" = 1
    15. Dim pty As Integer = my_circlemargin + (my_circlediameter + my_circlemargin) * (y - 1) 'my_circlediameter = Durchmesser eines "LEDs" = 7
    16. graph.FillEllipse(sb, ptx, pty, my_circlediameter, my_circlediameter)
    17. Next
    18. Next
    19. graph.Dispose()
    20. End Sub
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    Wie viele LEDs werden denn dargestellt? 2000x1000 klingt für mich Utopisch - soviel passt nichtmal auf den Bildschirm (wenn eine 7 Pixel Durchmesser hat)^^

    Ein Anfang wäre es z.B. das 'New SolidBrush(...)' außerhalb der Schleife zu deklarieren - also einen sbOn und einen sbOff und innerhalb der Schleife weist du sb nur noch einen dieser beiden zu - schon sparst du einen Haufen Konstruktoraufrufe.
    @FreakJNS:
    Ja das stimmt schon, 2000 x 1000 werden niemals dargestellt werden, aber ich brauchte ein Maximum, weil sonst my_status(x,y) = Nothing ist, und nicht wie hier my_status(x,y) = False

    Okay, werde ich beheben :)
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    Äh wat?
    Wenn my_status 20x30 'groß' ist, dann wird auch nur 20x30 gezeichnet, weil du in der Paint-Routine die ArrayDimension abfragst. Außerdem ist Boolean ein Wertetyp - der ist NIEMALS Nothing, sondern entweder True, oder False.
    Momentan zeichnest du also ein Fußballfeld obwohl du nur eine Briefmarke sehen willst^^
    Ups, dat fail :D Ja das werde ich wohl schnell mal beheben :D

    EDIT: I.was läuft bei der Variablendeklaration falsch:

    VB.NET-Quellcode

    1. Dim my_width As Integer
    2. Dim my_height As Integer
    3. Dim my_circlediameter As Integer = 10
    4. Dim my_circlemargin As Integer = 1
    5. Dim my_backcolor As Color = Color.FromArgb(20, 20, 20)
    6. Dim my_activecolor As Color = Color.Green
    7. Dim my_inactivecolor As Color = MixColors(my_backcolor, my_activecolor, 0.7)
    8. Dim my_status As Boolean(,) 'WIRD ÜBERSPRUNGEN


    Alle Variablen werden deklariert, aber nach my_inactivecolor starten die Form_Load , Form_Resize Events und die Properties, dann das Form_Paint Event und da crasht das dann, weil ja auf my_status zugegriffen wird aber das ja noch Nothing ist weil das nicht deklariert wurdee ?(
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „ThePlexian“ ()

    VB.NET-Quellcode

    1. graph.Dispose()
    Bist du noch zu retten?
    Du kannst doch nicht einfach ein Graphics disposen, was du nicht selbst erstellt hast!

    Stell dir vor, das Control will damit noch weitere Zeichnungen durchführen!

    und viel performance gewinnst du auch, wenn du alle Zeichnungen in einer Farbe in einem GraphicsPath anlegst.
    Dann brauchst du in der ZeichenRoutine nur noch 2 GraphicsPathes zu zeichnen - pro Farbe einen.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „ErfinderDesRades“ ()

    @ErfinderDesRades:
    Ups... Ich dachte das geht, weil das ja mein eigenes Control ist und nicht ein vorgelegtes... Sorry ^^

    Okay ich habe GraphicPaths zwar noch nicht benutzt aber das dürfte ja nicht zu schwer sein. Danke!

    @FreakJNS:
    Das mit der Klasse ist eine gute Idee, aber wie bezeichne ich die dann die Klasse? Wäre Protected richtig, weil ich ja nur will das mein UserControl darauf zugreifen kann, und der Nutzer der dll nicht.
    Auch an dich Danke!
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    Dassis generell die Idee des Dispose-Patterns, dass ein Objekt die Resourcen, die es selbst belegt, auch selbst wieder freigibt. Die Betonung hier auf selbst.
    Du hast keinen Code geschrieben, der ein Graphics-Objekt erstellt, dann schreib auch keinen, der ein Graphics zerstört.
    Offensichtlich wird innerhalb des Controls ein Graphics erstellt, und dann geht man davon aus, dasses auch innerhalb des Controls wieder zerstört wird.

    Theoretisch ist das möglich, dass die eine Klasse Resourcen einer anneren freigibt, aber praktisch wäre das total unübersichtlich.
    So vielen Dank an Alle, hat prima geklappt, hier noch mal mein Code:

    VB.NET-Quellcode

    1. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    2. Dim graph As Graphics = e.Graphics
    3. 'Background
    4. graph.FillRectangle(New SolidBrush(my_backcolor), 0, 0, Me.Width, Me.Height)
    5. 'Circles
    6. Dim graphpath_active As New GraphicsPath
    7. Dim graphpath_inactive As New GraphicsPath
    8. Dim sb_active As New SolidBrush(Color.FromArgb(230, my_activecolor))
    9. Dim sb_inactive As New SolidBrush(Color.FromArgb(230, my_inactivecolor))
    10. For x As Integer = 1 To HorizontalDots
    11. For y As Integer = 1 To VerticalDots
    12. Dim ptx As Integer = my_circlemargin + (my_circlediameter + my_circlemargin) * (x - 1)
    13. Dim pty As Integer = my_circlemargin + (my_circlediameter + my_circlemargin) * (y - 1)
    14. If my_status(x, y) = False Then
    15. graphpath_inactive.AddEllipse(ptx, pty, my_circlediameter, my_circlediameter)
    16. Else
    17. graphpath_active.AddEllipse(ptx, pty, my_circlediameter, my_circlediameter)
    18. End If
    19. Next
    20. Next
    21. graph.FillPath(sb_active, graphpath_active)
    22. graph.FillPath(sb_inactive, graphpath_inactive)
    23. End Sub
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais