?
Solved

Trying to draw a simple line in VB .net

Posted on 2010-09-22
6
Medium Priority
?
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

719 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