Quantcast
Channel: paint event on picturebox
Viewing all articles
Browse latest Browse all 48

paint event on picturebox

$
0
0

Hy, 

thank for your code that could be used in some other application.

Next gif shows a VB6 program that (if you pay attention) add point after point of a physical variable:

I can do the same with Visual Studio, the difference is that with VB6 i write only:

form1.PictureBox.PSet (x,y)

With Visual Studio I have to write all the drawing (points). 

Regards

PS The first time I ran it they were the same then I had the wrong image posted and ran it again. Now they are slightly different. My system is slower right now.

You check it.

Over all they are the same. If done at the same time. Its what I have been saying.

Here is your basic example one that draws one point at a time on a bitmap that is shown in the picturebox.image and one with redrawing 10000 points in paint event.

Compare times for 10,000 points:

Paint    Bitmap

1597    1562 secs

So now compare to drawing 10000 points in your vb6?

PS you can still speed them both up using a graphicsbuffer and a thread timer as previously mentioned.

Les is correct you can just filter ie draw every 100th point for most.

Try it with a million points and let us know.

The examples make all the controls just copy and paste into an empty form. Change the form name as required.

'draw one line each tick on bitmap

Public Class Form7 Private WithEvents Timer1 As New Timer With {.Interval = 10} Private WithEvents Button1 As New Button With {.Parent = Me, .Text = "Start", .Location = New Point(20, 20), .ForeColor = Color.AntiqueWhite, .BackColor = Color.ForestGreen} Private WithEvents PictureBox1 As New PictureBox With {.Parent = Me, .BackColor = Color.Black, .BorderStyle = BorderStyle.FixedSingle} Private deltax, deltay As Single Private offset As Single Private count As Integer Private points(10000) As Single Private myPen As Pen = New Pen(Drawing.Color.White, 3) Private Sw As New Stopwatch Private Frames As Integer Private PicBmp As Bitmap Private PicG As Graphics Private x1 As Single = 0 Private Sub Form6_Load(sender As Object, e As EventArgs) Handles Me.Load Text = "Draw on Bitmap" ClientSize = New Size(500, 300) Form6_Resize(0, Nothing) End Sub Private Sub Form6_Resize(sender As Object, e As EventArgs) Handles Me.Resize Dim border As Integer = 20 PictureBox1.Top = Button1.Bottom + border PictureBox1.Left = border PictureBox1.Width = ClientSize.Width - (2 * border) PictureBox1.Height = ClientSize.Height - (PictureBox1.Top + border) PicBmp = New Bitmap(PictureBox1.ClientSize.Width, PictureBox1.ClientSize.Height) PicG = Graphics.FromImage(PicBmp) deltay = PicBmp.Height deltax = CSng(PicBmp.Width / points.Length) offset = CSng(PicBmp.Height / 2) If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose() PictureBox1.Image = PicBmp End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click x1 = 0 count = 0 Frames = 0 Sw.Reset() Sw.Start() Timer1.Interval = 10 Timer1.Enabled = True End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Frames += 1 Dim fps As String = "0.0" Static nexttime As Long count = count + 1 points(count) = CSng((deltay * 0.3 * Math.Sin((10 / points.Length) * count)) + offset) If count > points.Length - 2 Then Timer1.Stop() With PicG 'show time every 500 ms fps = (Frames / (nexttime / 1000)).ToString("f1") If Sw.ElapsedMilliseconds > nexttime Then nexttime = Sw.ElapsedMilliseconds + 500 End If .FillRectangle(Brushes.Black, 0, 0, PictureBox1.ClientSize.Width, 30) .DrawString(count.ToString & " - " & fps & " fps " & (nexttime / 100).ToString("f0") & " secs", Me.Font, Brushes.White, 10, 10) If count > 1 Then .DrawLine(Pens.Red, x1, points(count - 1), x1 + deltax, points(count)) End If x1 += deltax End With PictureBox1.Invalidate() End Sub End Class


'draw all lines in paint each tick
Public Class Form6
    Private WithEvents Timer1 As New Timer With {.Interval = 10}
    Private WithEvents Button1 As New Button With {.Parent = Me, .Text = "Start",
            .Location = New Point(20, 20), .ForeColor = Color.AntiqueWhite, .BackColor = Color.ForestGreen}
    Private WithEvents PictureBox1 As New PictureBox With {.Parent = Me,
        .BackColor = Color.Black, .BorderStyle = BorderStyle.FixedSingle}

    Private deltax, deltay As Single
    Private offset As Single
    Private count As Integer
    Private points(10000) As Single
    Private myPen As Pen = New Pen(Drawing.Color.White, 3)
    Private Sw As New Stopwatch
    Private Frames As Integer
    Private x1 As Single = 0

    Private Sub Form6_Load(sender As Object, e As EventArgs) Handles Me.Load
        ClientSize = New Size(500, 300)
        Text = "Draw in Paint"
        Form6_Resize(0, Nothing)
    End Sub

    Private Sub Form6_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Dim border As Integer = 20

        PictureBox1.Top = Button1.Bottom + border
        PictureBox1.Left = border
        PictureBox1.Width = ClientSize.Width - (2 * border)
        PictureBox1.Height = ClientSize.Height - (PictureBox1.Top + border)


        deltay = PictureBox1.ClientSize.Height
        deltax = CSng(PictureBox1.ClientSize.Width / points.Length)
        offset = CSng(PictureBox1.ClientSize.Height / 2)


    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        x1 = 0
        count = 0
        Frames = 0
        Sw.Reset()
        Sw.Start()
        Timer1.Interval = 10
        Timer1.Enabled = True


    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Frames += 1


        points(count) = CSng((deltay * 0.3 * Math.Sin((10 / points.Length) * count)) + offset)

        count = count + 1

        If count > points.Length - 2 Then Timer1.Stop()


        PictureBox1.Invalidate()

    End Sub

    Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        Dim fps As String = "0.0"
        Static nexttime As Long

        With e.Graphics

            'show time every 500 ms
            fps = (Frames / (nexttime / 1000)).ToString("f1")

            If Sw.ElapsedMilliseconds > nexttime Then
                nexttime = Sw.ElapsedMilliseconds + 500
            End If

            .FillRectangle(Brushes.Black, 0, 0, PictureBox1.ClientSize.Width, 30)
            .DrawString(count.ToString & " - " &
                 fps & " fps  " & (nexttime / 100).ToString("f0") & " secs",
                 Me.Font, Brushes.White, 10, 10)

            x1 = 0

            If count > 1 Then
                For i As Integer = 1 To count - 1
                    x1 += deltax

                    .DrawLine(Pens.Red, x1, points(i - 1), x1 + deltax, points(i))

                Next
            End If
        End With
    End Sub
End Class





Viewing all articles
Browse latest Browse all 48

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>