Solved

Drawing a triangle in a picture box

Posted on 2007-03-26
7
1,287 Views
Last Modified: 2012-06-21
Hi there

I am trying to draw a triangle in a picture box.  I don't seem to get an error but nothing is displaying in the picture box.  Does anyone know what I am doing wrong?  I have tried pic.refresh and that doesn't seem to do anything
     
  Dim poly(3) As PointF ', NumCoords As Long, hBrush As Long, hRgn As Long

                poly(1).X = 0
                poly(1).Y = 0
                poly(2).X = 0
                poly(2).Y = pic.Height
                poly(3).X = pic.Width
                poly(3).Y = pic.Height

            'Get PictureBox's Graphics Object
            Dim gr As Graphics = pic.CreateGraphics
            Dim pn As Pen = Pens.AliceBlue
            gr.DrawPolygon(pn, poly)
            pic.Refresh()
            gr.Dispose()

Thanks for your help in advance
0
Comment
Question by:DavidGreenfield
  • 2
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
dctuck earned 250 total points
ID: 18792030
It's because the image gets repainted with its default contents whenever a refresh occurs.

If you put the code inside the Paint event for your pic it should work. e.g:

Private Sub pic_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pic.Paint
     Dim poly(3) As PointF ', NumCoords As Long, hBrush As Long, hRgn As Long
    poly(1).X = 0
    poly(1).Y = 0
    poly(2).X = 0
    poly(2).Y = pic.Height
    poly(3).X = pic.Width
    poly(3).Y = pic.Height
    'Get PictureBox's Graphics Object
    Dim gr As Graphics = pic.CreateGraphics
    Dim pn As Pen = Pens.AliceBlue
    gr.DrawPolygon(pn, poly)
    pic.Refresh()
    gr.Dispose()
End Sub
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 18793475
Lol dctuck...it isn't really any better to place the code into the Paint() event when you left the call to CreateGraphics() in there...

...also, the call to pic.Refresh() in the Paint() event will lead to an infinite refreshing loop and alot of flicker...

DavidGreenfield...Arrays start at index 0 (zero) in .Net.

(On a side note...with the default picturebox background color, Pens.AliceBlue was barely visible on my system so I switched to Pens.Black)

Use the graphics supplied in the "e" parameter of the Paint() event.  If you move the Point Array to the form level then you can update the points dynamically and cause the triangle to change.

For example:

Public Class Form1

    Private poly() As Point

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        poly = New Point() { _
            New Point(0, 0), _
            New Point(0, pic.Height - 1), _
            New Point(pic.Width - 1, pic.Height - 1)}
    End Sub

    Private Sub pic_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pic.Paint
        e.Graphics.DrawPolygon(Pens.Black, poly)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        poly(0).X = 25
        poly(2) = New Point(pic.Width / 2, pic.Height / 2)
        pic.Refresh()
    End Sub

End Class
0
 
LVL 7

Expert Comment

by:dctuck
ID: 18793509
Idle_Mind - I know, I forgot to adjust that: I checked the code in VB and then went and copy and pasted the original code without changing it.... d'oh!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18793530
Hehe...I can't say I've never done a similar thing.   =)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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