Solved

Trying to draw a simple line in VB .net

Posted on 2010-09-22
6
347 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Syntax 9 36
Gridview selected row 9 44
.NET tools for adding thread safety to a web app? 3 33
VB.net capture  result of a SQL query in variables 3 7
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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