title: How to draw a rectangle over an image and save it in VB.Net
body: <p>I have used the following code to draw a rectangle on the image buI i an not able to save the image with the highlighted rectangle. Only the original image is saved not the image with the rectangle. Please help me with this.</p>
  
  <pre><code>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 &lt; 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 &lt; 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 &gt; 0 And SelectionBoxObj.Height &gt; 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
  </code></pre>