Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET Communications (http://www.xtremevbtalk.com/-net-communications/)
-   -   Drawing in picturebox (http://www.xtremevbtalk.com/-net-communications/327058-drawing-picturebox.html)

christiaan01 05-23-2014 07:47 AM

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.

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"

        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
                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, " ")

                            '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)

                            End If

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

                    Exit Sub
                End If
            End If
        draw = False
        Me.Label1.Text = vbNullString
    End Sub

Gruff 05-23-2014 09:37 AM

Welcome to the forum. Do not forget to read the forum Posting Guidelines.


I have not tried running your code, but one thing leaps out at me.
You are not using the PicutureBox.Paint event to paint your graphics.

When you use the paint event Windows itself will redraw your graphics whenever it needs to. This is the easiest and most fool proof way of painting in winforms.

When you try to create a graphics object on your own you take on all the myriad things that can go wrong yourself.

Imports System.IO Public Class Form1 Dim Circles As New List(Of clsCircle) Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click ' Load Circle Data from a text file If File.Exists("C:\Circles.txt") Then Dim Lines() As String = File.ReadAllLines("C:\Circles.txt") For Each Line As String In Lines Dim Segs() As String = Line.Split(","c) ' Check for x,y,radius If Segs.Count = 3 Then ' We really should make sure these are integers before we continue... Dim Circle As New clsCircle(CInt(Segs(0)), CInt(Segs(1)), CInt(Segs(2))) Circles.Add(Circle) End If Next ' Tell windows the picturebox is dirty so it redraws the first time. If Circles.Count > 0 Then PictureBox1.Invalidate() End If End Sub Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint Dim g As Graphics = e.Graphics ' If the Circles list contains objects then draw them For Each Circle as clsCircle In Circles With Circle ' The ellipse class in VB.NET draws the ellipse inside a rectangle. ' So we create the rectangle about the center point. Dim Rect As New Rectangle(.X - .Radius, .Y - .Radius, .Radius * 2, .Radius * 2) g.DrawEllipse(Pens.Blue, Rect) End With Next End Sub End Class Public Class clsCircle ' This shortened form of defining a properties ' is supported in VB.NET 2010 and above. Public Property X As Integer = 0 Public Property Y As Integer = 0 Public Property Radius As Integer = 0 Public Sub New(XValue As Integer, YValue As Integer, RadiusValue As Integer) X = XValue Y = YValue Radius = RadiusValue End Sub End Class

All times are GMT -6. The time now is 06:17 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2018 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.