View Single Post
 
Old 05-23-2014, 07:47 AM
christiaan01 christiaan01 is offline
Newcomer
 
Join Date: May 2014
Posts: 1
Default Drawing in picturebox

Im drawing circles in a picturebox, the x and y values are imported from a file. For some reason they shift on my picturebox, the first row draws fine but when it updates the circles deviate from the x and y in the file.
Code:
Private Sub DrawToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DrawToolStripMenuItem.Click
        draw = True
        Dim mret
        If PictureBox1.Image Is Nothing Then
            mret = MessageBox.Show("No image is loaded.", "Draw", MessageBoxButtons.OK, MessageBoxIcon.Error)
            If mret = vbOK Then Exit Sub
        End If

        If mPointsRead Is Nothing Then
            mret = MessageBox.Show("No cordinates loaded.", "Draw", MessageBoxButtons.OK, MessageBoxIcon.Error)
            If mret = vbOK Then Exit Sub
        End If

        Me.Label1.ForeColor = Color.Green
        Me.Label1.Text = vbNullString
        Me.Label1.Text = "Drawing circles"
        Me.Label1.Refresh()

        frmdate = ToolStripTextBox1.Text
        todate = ToolStripTextBox2.Text

        Dim LineOfText As String
        Dim aryTextFile(), aryLineText() As String

        Dim FILE_NAME As String = "C:\Users\Tiaan home\Documents\test.dat"

        Dim objReader As New System.IO.StreamReader(FILE_NAME)

        Dim m As Double
        Dim p As Integer

        Dim x, y As Integer
        Dim temp(), tt As String
        Dim tdate As Date
        Dim fdate As Date
        Dim crdate As Date
        Dim pen As New System.Drawing.Pen(Color.Red, 4)
        Dim g As System.Drawing.Graphics
        Dim n As Integer
        Integer.TryParse(TextBox1.Text, n)
        LineOfText = objReader.ReadToEnd
        aryTextFile = LineOfText.Split(Environment.NewLine)

        For inti = 0 To UBound(aryTextFile) - 1   '.Length - 1
            If aryTextFile(inti) = vbNullString Then
            Else
                tdate = todate
                fdate = frmdate
                aryLineText = aryTextFile(inti).Split(",")
                temp = Split(aryLineText(0), " ")
                tt = Replace(temp(0), """", "")
                crdate = tt
                If crdate >= fdate And crdate <= todate Then
                    If mLoad Then
                        For intj = 0 To mPointsRead.Count - 1
                            aryLineText(2 + intj) = aryLineText(2 + intj) * 1000
                            Me.Label1.Text = vbNullString
                            Me.Label1.Text = "Circle on " & Replace(aryLineText(0), vbLf, " ")
                            Me.Label1.Refresh()

                            'MessageBox.Show("From:" & frmdate & ",    To:" & todate & vbCrLf & "Current date:" & aryLineText(0) & vbCrLf & "Radius:" & aryLineText(2))
                            If Double.TryParse(aryLineText(2 + intj), m) Then

                                If PictureBox1.Image Is Nothing Then Exit Sub
                                '  g = Graphics.FromImage(PictureBox1.Image)
                                g = Graphics.FromImage(PictureBox1.Image)
                                pen = New Pen(Color.Red, 1)
                                If m < 0 Then m = m * -1
                                p = CType(m, Integer)
                                temp = mPointsRead("Point " & intj).ToString.Split(",")
                                x = CType(temp(0), Integer)
                                y = CType(temp(1), Integer)
                                g.DrawEllipse(pen, x, y, p, p)
                                g.FillEllipse(Brushes.Red, x, y, p, p)
                                PictureBox1.Refresh()

                            End If
                        Next

                        ' g.Clear(PictureBox1.BackColor)
                    End If
                Else
                    'GoTo N
                    draw = False
                    Me.Label1.Text = vbNullString
                    Me.Label1.Refresh()

                    Exit Sub
                End If
            End If
            System.Threading.Thread.Sleep(n)
        Next
        draw = False
        Me.Label1.Text = vbNullString
        Me.Label1.Refresh()
    End Sub
Reply With Quote