0

I've been working on a moveable rectangle to compute the ROI of an Image in VB 2010, Since i'm new to VB, I've been able to create a rectangle, But it doesnot appear on my uploaded image. It appears but not on the image. By my listed code below 1. How do I get the rectangle to display on the image. 2. How do get the rectangle to become moveable through out the image. I will be grateful. thanks.

 Private Sub ROIToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ROIToolStripMenuItem.Click
    Dim G As Graphics
    G = PictureBox1.CreateGraphics
    G.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
    G.FillRectangle(Brushes.Silver, ClientRectangle)
    Dim P As Point
    Dim Box As Rectangle
    P.X = 1
    P.Y = 1
    Dim S As Size
    S.Width = 100
    S.Height = 20
    Box = New Rectangle(P, S)
    G.DrawRectangle(Pens.Red, Box)
4

1 回答 1

0

试试下面的代码

Dim G As Graphics
Dim uploadedBmp1, backgroundBmp1 As Bitmap

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    uploadedBmp1 = New Bitmap("C:\Windows\winnt.bmp")
    backgroundBmp1 = New Bitmap(uploadedBmp1.Width, uploadedBmp1.Height)

    G = Graphics.FromImage(backgroundBmp1)
    PictureBox1.Image = backgroundBmp1
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    DrawRectangleOnImage(New Point(0, 0), New Size(100, 50))
End Sub

Private Sub DrawRectangleOnImage(ByVal point1 As Point, ByVal size1 As Size)
    G.Clear(Color.White)
    G.DrawImage(uploadedBmp1, 0, 0, uploadedBmp1.Width, uploadedBmp1.Height)

    Dim rectangle1 As New Rectangle(point1, size1)
    G.DrawRectangle(Pens.Red, rectangle1)
    PictureBox1.Invalidate()
End Sub

要制作一个移动矩形,只需使用您想要的参数调用 DrawRectangleOnImage 子。它将自动清除之前的快照并重新绘制所有内容。

于 2013-07-01T16:22:06.417 回答