Click here to Skip to main content
15,886,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a picture box with a loaded image. I have 2 ways I want to draw circles on the image. The first is by clicking on the image with the mouse. Once the mouse click fires, it stores the points to the database and draws the circle. This works Fine.

VB
Private Sub InjectionSites_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FaceImage.MouseClick
        Dim sSql As String
        Dim MyPoint As New Point

        MyPoint.X = e.X
        MyPoint.Y = e.Y

        MarkLocationOnFace(MyPoint)

        sSql = "insert into Injections (x, y, custid, date, units)"
        sSql = sSql & " values (" & e.X & ", " & e.Y & ", 1, '" & Now() & "', 2)"
        Conn.Execute(sSql)

    End Sub

    Private Sub MarkLocationOnFace(ByVal myPoint As Point)
        Dim p As New System.Drawing.Pen(Brushes.Black, 4)
        Dim g As System.Drawing.Graphics

        g = FaceImage.CreateGraphics
        g.DrawEllipse(p, myPoint.X, myPoint.Y, 10, 10)

    End Sub



The second, I want to recall the points that were stored to the database and plot them back to the image as they were originally drawn. It appears the subroutine is working, but once the picturebox appears, the circles are not on the image.

VB
Private Sub LoadPreviousVisit(ByVal MyDate As Date, ByVal CustID As Decimal)
        Dim sSql As String
        Dim rs As New ADODB.Recordset
        Dim mypoint As New Point

        sSql = "Select * from Injections where CustID = " & CustID
        sSql = sSql & " and CONVERT(VARCHAR(10),date,101) = '"
        sSql = sSql & MyDate.ToString("MM/dd/yyyy") & "'"
        rs.Open(sSql, Conn)

        Do While Not rs.EOF
            mypoint.X = rs("X").Value
            mypoint.Y = rs("Y").Value

            MarkLocationOnFace(mypoint)
            rs.MoveNext()
        Loop
        FaceImage.SendToBack()
    End Sub


What I have tried:

Sending the Picture box to the back does not appear to work either.
Posted
Updated 4-Feb-18 0:08am
v2
Comments

1 solution

That's because you are drawing on the picturebox, but not in the right place. There are two ways to fix this:
1) Draw onto the Image that the PictureBox control that the PictureBox control displays, and call Invalidate on the Picturebox.
2) Draw onto the PictureBox itself instead in it's Paint event using the Graphics object passed to you in the PaintArge parameter. Again Invalidate the PictureBox each time you add a point to draw it.

Me? I'd add a List of Point objects which I add each new location to, and draw them in the PictureBox Paint event (it's a lot faster than going to the DB each time, and Paint handlers should be as short as possible)

Whichever way you go, you have to be aware that Graphics contexts are extremely scarce resources: if you create one (as you do each time you call MarkLocationOnFace) then you are responsible for ensuring it is correctly Disposed, or your app will crash with an "out of memory" exception long before the "real" memory is exhausted.
 
Share this answer
 
Comments
Myeager1249 4-Feb-18 19:31pm    
Thanks for your help! This solved the problem and you're right, Building the list of points is a much better plan.
OriginalGriff 5-Feb-18 1:52am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900