Drawing in picturebox
Drawing in picturebox
Drawing in picturebox
Drawing in picturebox
Drawing in picturebox
Drawing in picturebox Drawing in picturebox Drawing in picturebox Drawing in picturebox Drawing in picturebox Drawing in picturebox Drawing in picturebox Drawing in picturebox
Drawing in picturebox Drawing in picturebox
Drawing in picturebox
Go Back  Xtreme Visual Basic Talk > > > Drawing in picturebox


Reply
 
Thread Tools Display Modes
  #1  
Old 05-23-2014, 08: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
  #2  
Old 05-23-2014, 10:37 AM
Gruff's Avatar
GruffDrawing in picturebox Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

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

christiaan01,

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.

Code:
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
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 05-23-2014 at 10:47 AM.
Reply With Quote
Reply

Tags
drawing, picturebox


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Drawing in picturebox
Drawing in picturebox
Drawing in picturebox Drawing in picturebox
Drawing in picturebox
Drawing in picturebox
Drawing in picturebox Drawing in picturebox Drawing in picturebox Drawing in picturebox Drawing in picturebox Drawing in picturebox Drawing in picturebox
Drawing in picturebox
Drawing in picturebox
 
Drawing in picturebox
Drawing in picturebox
 
-->