Solved

Trying to draw a simple line in VB .net

Posted on 2010-09-22
6
345 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

11 Experts available now in Live!

Get 1:1 Help Now