如何使用数据库中的点在VB.NET中的图片框上绘制圆圈? [英] How do I draw circles on a picturebox in VB.NET using points from a database?

查看:226
本文介绍了如何使用数据库中的点在VB.NET中的图片框上绘制圆圈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个装有图片的图片框。我有2种方法可以在图像上绘制圆圈。第一种是用鼠标点击图像。一旦点击鼠标,它就会将点存储到数据库并绘制圆圈。这很好用。



 私人  Sub  InjectionSites_MouseClick( ByVal  sender  As  对象 ByVal  e  As  System.Windows.Forms.MouseEventArgs)句柄 FaceImage.MouseClick 
Dim sSql As < span class =code-keyword> String
Dim MyPoint As < span class =code-keyword> New Point

MyPoint.X = eX
MyPoint.Y = eY

MarkLocationOnFace(MyPoint)

sSql = 插入注射(x,y,custid,日期,单位)
sSql = sSql& values(& eX& & eY& ,1 ,'& Now()& ',2)
Conn.Execute(sSql)

结束 Sub

私有 Sub MarkLocationOnFace( ByVal myPoint As Point)
Dim p As System.Drawing.Pen(Brushes.Black, 4
< span class =code-keyword> Dim g As System.Drawing.Graphics

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

结束 Sub







第二,我想回忆一下存储在数据库中的点,并将它们绘制回原来的图像中画。看起来子程序正在运行,但是一旦图片框出现,圆圈就不在图像上了。



 私有  Sub  LoadPreviousVisit( ByVal  MyDate 作为 日期 ByVal  CustID 正如 十进制
Dim sSql 正如 字符串
Dim rs 正如 ADODB.Recordset
Dim mypoint 作为

sSql = 从注入中选择*,其中CustID =& CustID
sSql = sSql& 和CONVERT(VARCHAR(10),date,101)='
sSql = sSql& MyDate.ToString( MM / dd / yyyy)& '
rs.Open(sSql,Conn)

rs.EOF
mypoint.X = rs( X)。值
mypoint.Y = rs( Y)。值

MarkLocationOnFace(mypoint)
rs.MoveNext()
循环
FaceImage.SendToBack()
结束 Sub





我尝试了什么:



将图片框发送到后面似乎也不起作用。

解决方案

< blockquote>那是因为你正在绘图框,但不是在正确的地方。有两种方法可以解决这个问题:

1)绘制PictureBox控件显示的PictureBox控件的Image,并在Picturebox上调用Invalidate。

2)Draw使用在PaintArge参数中传递给您的Graphics对象,在PictureBox本身而不是它的Paint事件中。每次添加一个点来绘制它时,再次使PictureBox无效。



我?我将添加一个Point对象列表,我将每个新位置添加到其中,并在PictureBox Paint事件中绘制它们(它比每次进入DB要快得多,并且Paint处理程序应尽可能短)



无论你走哪条路,都要注意图形上下文是非常稀缺的资源:如果你创建一个(就像你每次调用MarkLocationOnFace时那样)那么你就要负责了确保它被正确处置,或者你的应用程序在真实内存耗尽之前就会因内存不足异常而崩溃。


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.

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.

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.

解决方案

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.


这篇关于如何使用数据库中的点在VB.NET中的图片框上绘制圆圈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆