如何在图像上绘制一个矩形并将其保存在VB.Net中

时间:2015-03-03 16:36:13

标签: vb.net image highlight

我使用以下代码在图像上绘制一个矩形,我无法使用突出显示的矩形保存图像。仅保存原始图像而不保存带有矩形的图像。请帮帮我。

Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
  If e.Button = Windows.Forms.MouseButtons.Left Then
    IsMouseDown = True
    SelectedObjPoint = New Point(e.X, e.Y)
  End If
End Sub

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
  If IsMouseDown = True Then
    If e.X < SelectionBoxObj.X Then
      SelectionBoxObj.X = e.X
      SelectionBoxObj.Width = SelectedObjPoint.X - e.X
    Else
      SelectionBoxObj.X = SelectedObjPoint.X
      SelectionBoxObj.Width = e.X - SelectedObjPoint.X
    End If

    If e.Y < SelectedObjPoint.Y Then
      SelectionBoxObj.Y = e.Y
      SelectionBoxObj.Height = SelectedObjPoint.Y - e.Y
    Else
      SelectionBoxObj.Y = SelectedObjPoint.Y
      SelectionBoxObj.Height = e.Y - SelectedObjPoint.Y
    End If
    Me.Refresh()
  End If
End Sub

Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
  IsMouseDown = False
End Sub

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
  If SelectionBoxObj.Width > 0 And SelectionBoxObj.Height > 0 Then
    Dim oGradientBrush As Brush = New Drawing.Drawing2D.LinearGradientBrush(SelectionBoxObj.RectangleF, SelectionBoxObj.FillColor, SelectionBoxObj.FillColor, Drawing.Drawing2D.LinearGradientMode.Vertical)
    e.Graphics.FillRectangle(oGradientBrush, SelectionBoxObj.RectangleF)

    Dim TempPen = New Pen(SelectionBoxObj.BorderLineColor, SelectionBoxObj.BorderLineWidth)
    TempPen.DashStyle = SelectionBoxObj.BorderLineType
    e.Graphics.DrawRectangle(TempPen, SelectionBoxObj.RectangleF.X, SelectionBoxObj.RectangleF.Y, SelectionBoxObj.RectangleF.Width, SelectionBoxObj.RectangleF.Height)
  End If
End Sub

Private Sub Highlight_Ok_Click(sender As Object, e As EventArgs) Handles Highlight_Ok.Click
  Dim datestring As String = Date.Now.ToString("yyyyMMddmmss")
  Dim path As String = "D:\capture screenshot temp"
  Dim currentsavepath As String = String.Format("{0}\capture_{1}.png", path, datestring)
  PictureBox1.Image.Save(currentsavepath, Imaging.ImageFormat.Bmp)
End Sub

1 个答案:

答案 0 :(得分:2)

您在PictureBox中绘制的矩形是临时图形。它显示当前图像,其上面绘有矩形。但是当您保存图像时,矩形不是图像的一部分。

您可以通过将绘图例程移动到将您传递给它的图形对象中绘制结果的过程来纠正您的情况:

Private Sub DrawHighlight(g As Graphics)
  If SelectionBoxObj.Width > 0 And SelectionBoxObj.Height > 0 Then
    Using br As New SolidBrush(SelectionBoxObj.FillColor)
      g.FillRectangle(br, SelectionBoxObj.RectangleF)
    End Using
    Using p As New Pen(SelectionBoxObj.BorderLineColor, SelectionBoxObj.BorderLineWidth)
      g.DrawRectangle(p, SelectionBoxObj.RectangleF)
    End Using
  End If
End Sub

现在您可以轻松更新屏幕了:

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
  DrawHighlight(e.Graphics)
End Sub

您可以保存最终结果:

Using bmp As New Bitmap(PictureBox1.Image)
  Using g As Graphics = Graphics.FromImage(bmp)
    DrawHighlight(g)
  End Using
  bmp.Save(currentsavepath, Imaging.ImageFormat.Bmp)
End Using