Reputation: 35
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.
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
Upvotes: 3
Views: 6959
Reputation: 81610
The rectangle you are drawing in the PictureBox is a temporary drawing. It's showing the current image with the rectangle drawn on top of it. But when you are saving the image, the rectangle isn't a part of the image.
You can remedy your situation by moving your drawing routine into a procedure that will draw the result in a graphic object you pass to it:
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
Now you can update the screen easily:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
DrawHighlight(e.Graphics)
End Sub
and you can save the final results:
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
Upvotes: 3