Solved

Trying to draw a simple line in VB .net

Posted on 2010-09-22
6
346 Views
Last Modified: 2012-05-10
Hi there...I am trying to draw a simple line on a picture box in my form in vb .net.
My code is pretty straight forward...But i do not see anything on my form...

Public Class Form1

    Dim gMyGraphics As Graphics ' the drawing surface
    Dim pMyPen As Pen ' used to draw the lines
    Dim bMyBrush As SolidBrush = New SolidBrush(Color.White) ' used to draw the background

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gMyGraphics = lineDisplay.CreateGraphics


        ' set up the drawing surface to draw in the picturebox
        gMyGraphics = lineDisplay.CreateGraphics

        ' set up a pen with the color ' - this pen will be drawing the lines
        pMyPen = New Pen(Color.Orange, 5)

        ' fill the picturebox with a background color
        bMyBrush.Color = Color.Blue
        gMyGraphics.FillRectangle(bMyBrush, 0, 0, lineDisplay.Width, lineDisplay.Height)

        gMyGraphics.DrawLine(pMyPen, 0, 0, 100, 50)
        'gMyGraphics.DrawLine(pMyPen, 0, CInt(lineDisplay.Height / 2), lineDisplay.Width, CInt(lineDisplay.Height / 2))


    End Sub
End Class

Open in new window

0
Comment
Question by:ioustinos_sarris
  • 3
  • 3
6 Comments
 

Author Comment

by:ioustinos_sarris
ID: 33736864
Ok i found out that if i put the whole code into a button click event everything works fine...
However i would like to have it on form load.

What can i do
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33737484
It's not appearing because you are using CreateGraphics() which is a temp surface and gets erased when the control refreshes.

To make your line persistent you either need to:
(1) Draw to a Bitmap and set that as the PictureBox Image.
(2) Draw using the Graphics supplied in the Paint() event of your PictureBox.

Here's an example of the second approach:
Public Class Form1



    Private Sub linedisplay_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles linedisplay.Paint

        Dim G As Graphics = e.Graphics

        ' set up a pen with the color ' - this pen will be drawing the lines

        Using pMyPen As New Pen(Color.Orange, 5)

            G.FillRectangle(Brushes.Blue, 0, 0, linedisplay.Width, linedisplay.Height)

            G.DrawLine(pMyPen, 0, 0, 100, 50)

        End Using

    End Sub



End Class

Open in new window

0
 

Author Comment

by:ioustinos_sarris
ID: 33742614
@idle_mind
1) As far as i searched (not too much) there are not that many readu function as drawline etc when it comes to bitmap. i have to draw pixel by pixel.

2) And how would i trigger that event?


thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33743701
For #1, you simply create a Graphics from your Bitmap then you have all the drawing methods at your disposal:

    Dim bmp As New Bitmap(width, height)
    Using G As Graphics = Graphics.FromImage(bmp)
        G.DrawLine(Pens.Black, 0, 0, width, height)
        ' ... etc ....
    End Using
    PictureBox1.Image = bmp

For #2, the Paint() event fires automatically whenever that control needs to be painted.
0
 

Author Comment

by:ioustinos_sarris
ID: 33754245
@idle_mind
you say "the Paint() event fires automatically whenever that control needs to be painted."

What does that mean?
I want to be able to write anywhere on my code gMyGraphics.drawLine(....)
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33754561
Sounds like you need to move the info about the lines to CLASS level and then force a repaint using "linedisplay.Refresh()".

If you explain more about your app I can probably give better advice...  =)

This example adds a random line each time the button is clicked and redraws the picturebox:
Public Class Form1



    Private R As New Random

    Private Segments As New List(Of Segment)

    Private Colors() As Color = {Color.Red, Color.Blue, Color.Green, Color.Yellow, Color.Purple}



    Private Class Segment



        Public PtA As Point

        Public ptB As Point

        Public Clr As Color



        Public Sub Paint(ByVal G As Graphics)

            Using P As New Pen(Me.Clr)

                G.DrawLine(P, Me.PtA, Me.ptB)

            End Using

        End Sub



    End Class



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim seg As New Segment

        seg.Clr = Me.Colors(Me.R.Next(0, Me.Colors.Length))

        seg.PtA = New Point(Me.R.Next(0, PictureBox1.Width), Me.R.Next(0, PictureBox1.Height))

        seg.ptB = New Point(Me.R.Next(0, PictureBox1.Width), Me.R.Next(0, PictureBox1.Height))

        Segments.Add(seg)



        PictureBox1.Refresh()

    End Sub



    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

        For Each seg As Segment In Segments

            seg.Paint(e.Graphics)

        Next

    End Sub



End Class

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now