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

paint event on picturebox

$
0
0

Hi

Here is yet another ..................

This again simulates incoming random data points.

This code example shows the data with a variable compression.

With a high compression, the following image shows results from 800 data points - it is still usable. However,compressing any further creates the effect I previously tried to explain where the graph is so compressed that all that can be seen is a 'block' of colour.

You were talking of a million data points, and indicated that such a large number of data points could all be viewed in one image - I can't imagine anything other than a solid block of colour in those circumstances!

Anyway here is the code (not very elegant as put together quickly)

' blank Form1
Option Strict On
Option Explicit On
Public Class Form1
  Dim WithEvents pan As New Panel
  Dim WithEvents HS As New HScrollBar
  Dim WithEvents TBx As New TrackBar
  Dim WithEvents TBy As New TrackBar
  Dim Lab, lab2 As New Label
  Dim pts As New List(Of PointF)
  Dim myPen As New Pen(Color.Blue, 4)
  Dim xOffset As Single = 0D
  Dim scx As Single = 1D
  Dim scy As Single = 0.5D

  Dim r As New Random

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Size = New Size(700, 500)
    DoubleBuffered = True
    ' set up various controls
    With pan
      .Width = ClientSize.Width - 20
      .Height = ClientSize.Height - 100
      .Location = New Point(10, 10)
      .BorderStyle = BorderStyle.FixedSingle
      .BackColor = Color.FromKnownColor(KnownColor.PaleGoldenrod)
      .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    End With
    With HS
      .Width = pan.Width
      .Height = 20
      .BackColor = Color.FromKnownColor(KnownColor.Pink)
      .Location = New Point(0, pan.Bottom - 28)
      .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
      .Maximum = 0
    End With
    With TBx
      .LargeChange = 1
      .SmallChange = 1
      .Maximum = 20
      .Minimum = 0
      .Location = New Point(pan.Left + 20, pan.Bottom + 8)
      .Width = pan.Width \ 2
      .Value = 10
      .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left
    End With
    With TBy
      .LargeChange = 1
      .SmallChange = 1
      .Minimum = 2
      .Maximum = 20
      .Location = New Point(TBx.Left, TBx.Bottom + 8)
      .Width = pan.Width \ 2
      .Value = 11
      .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left
    End With
    With Lab
      .AutoSize = True
      .Location = New Point(TBy.Right + 20, TBy.Top - 10)
      .Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
      .BorderStyle = BorderStyle.FixedSingle
      .BackColor = Color.FromKnownColor(KnownColor.PaleGoldenrod)
      .Font = New Font("Arial", 14)
      .Text = "Data Points"
    End With
    With lab2
      .AutoSize = True
      .Location = New Point(TBx.Right + 20, TBx.Top)
      .Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
      .Font = New Font("Arial", 14)
      .Text = "Click Panel to Pause/Resume"
    End With

    pan.Controls.Add(HS)
    Controls.AddRange({pan, TBx, TBy, Lab, lab2})
    ' -----------------------------

    scx = CSng(TBx.Value / 10)
    scy = CSng(TBy.Value / 5)

    Timer1.Interval = 120
    Timer1.Enabled = True

  End Sub
  Private Sub Pan_Paint(sender As Object, e As PaintEventArgs) Handles pan.Paint
    If pts.Count < 2 Then Exit Sub
    e.Graphics.TranslateTransform(xOffset, pan.Height \ 5)
    e.Graphics.ScaleTransform(scx, scy)
    e.Graphics.DrawCurve(myPen, pts.ToArray)
  End Sub
  Private Sub pan_Resize(sender As Object, e As EventArgs) Handles pan.Resize
    HS.LargeChange = pan.Width - 40
    HS.SmallChange = pan.Width \ 6
  End Sub
  Private Sub HS_Scroll(sender As Object, e As ScrollEventArgs) Handles HS.Scroll
    xOffset = -HS.Value
    pan.Invalidate()
  End Sub
  Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Static c As Integer = 0
    ' Simulate incoming data points
    Static x As Integer = 0
    x += Timer1.Interval \ 100
    pts.Add(New Point(x * 5, r.Next(-20, 100)))
    HS.Maximum = CInt(pts.Last.X * scx)
    Lab.Text = "Pts: " & pts.Count.ToString
    If c Mod 4 = 0 Then pan.Invalidate()
    ' set slight time diff for arrival
    Timer1.Interval = r.Next(150, 300)
    c += 1
  End Sub
  Private Sub Tbx_Scroll(sender As Object, e As EventArgs) Handles TBx.Scroll
    scx = CSng(TBx.Value / 10)
    If scx < 0.01 Then scx = 0.01
  End Sub
  Private Sub Tby_Scroll(sender As Object, e As EventArgs) Handles TBy.Scroll
    scy = CSng(TBy.Value / 5)
    If scy < 0.01 Then scy = 0.01
  End Sub
  Private Sub pan_MouseClick(sender As Object, e As MouseEventArgs) Handles pan.MouseClick
    Timer1.Enabled = Not Timer1.Enabled
  End Sub
End Class


Regards Les, Livingston, Scotland



Viewing all articles
Browse latest Browse all 48

Trending Articles



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