0

I draw a rectangle on a picture and want to save the picture it does not keep me well. The rectangle looks smaller and in the wrong position.

Here is the code:

Public Class Form10
    Dim G_RegistInfoPathSinglePage = "D:\0_0_1.TIF"
    Dim SelectRect As Rectangle = New Rectangle()
    Dim _pen As Pen = New Pen(Color.Purple, 4)
    Dim _brush As SolidBrush = New SolidBrush(Color.Pink)
    Dim _ControlPressed As Boolean = False

    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        _ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
    End Sub

    Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
        _ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
    End Sub


    Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles picOriginal.MouseDown
        SelectRect.Location = e.Location

        SelectRect.Size = New Size(0, 0)
    End Sub

    Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles picOriginal.MouseMove
        If (e.Button = MouseButtons.Left) Then
            ControlPaint.DrawReversibleFrame(picOriginal.RectangleToScreen(SelectRect), picOriginal.BackColor, FrameStyle.Dashed)
            SelectRect.Width = e.X - SelectRect.X
            SelectRect.Height = e.Y - SelectRect.Y
            ControlPaint.DrawReversibleFrame(picOriginal.RectangleToScreen(SelectRect), picOriginal.BackColor, FrameStyle.Dashed)
        End If
    End Sub

    Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles picOriginal.MouseUp

        If (e.Y < SelectRect.Y) Then
            SelectRect.Location = If(SelectRect.Location.X > e.X,
                                     New Point(e.X, e.Y), New Point(SelectRect.X, e.Y))
            SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
        Else
            If SelectRect.Location.X > SelectRect.Right Then
                SelectRect.Location = New Point(e.X, SelectRect.Y)
                SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
            End If
        End If

        If _ControlPressed Then
            Dim _InflatedRect As Rectangle = New Rectangle(SelectRect.Location, SelectRect.Size)
            _InflatedRect.Inflate(CInt(_pen.Width / 2), CInt(_pen.Width / 2))
            picOriginal.Invalidate(_InflatedRect)
        Else
            picOriginal.Invalidate()
        End If

    End Sub

'paint rectangle
    Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles picOriginal.Paint


        e.Graphics.DrawRectangle(_pen, SelectRect)

        SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
        e.Graphics.FillRectangle(_brush, SelectRect)


    End Sub

'if Pressing ctrl + S switches to a function that saves the image
    Private Sub main_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.S AndAlso e.Modifiers = Keys.Control) Then

            SaveDrawRectengle()

            MsgBox("Save successfully")
        End If
    End Sub

'save to image as tif
    Public Sub SaveDrawRectengle()

        Dim l_OriginalImage As New System.IO.FileStream(G_RegistInfoPathSinglePage, IO.FileMode.Open, IO.FileAccess.Read)
        Dim OriginalImage = System.Drawing.Image.FromStream(l_OriginalImage)

        Dim Bfr As Bitmap = New Bitmap(OriginalImage)
        picOriginal.BackgroundImage = Bfr
        Dim _image = picOriginal.BackgroundImage

        Dim overlay As New Bitmap(_image.Width, _image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        Dim g As Graphics = Graphics.FromImage(overlay)

        g.DrawRectangle(_pen, SelectRect)
        SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
        g.FillRectangle(_brush, SelectRect)


        g.Dispose()

        picOriginal.Image = overlay

        l_OriginalImage.Close()


        Dim Tmpimg As Image = New Bitmap(300, 100)

        Tmpimg = CType(Bfr, Image)
        Dim Tmpg As Graphics = Graphics.FromImage(Tmpimg)

        Tmpg.DrawImage(CType(overlay, Image), New Point(10, 10))
        Tmpimg.Save("D:\852.tif")

    End Sub
End Class
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
ayala
  • 1
  • 2
  • 3
    Files don't magically become a specific type just because you set the extension. If you want to save an `Image` in TIFF format then you need to specify that. If you had read the documentation as you should have then you'd know that you're actually saving it in PNG format. Call the overload of `Save` that takes an `ImageFormat` value and specify `Tiff`. – jmcilhinney Sep 16 '20 at 09:51
  • 2
    Finally, there's really way too much code there. Please read [this](https://stackoverflow.com/help/minimal-reproducible-example) and edit your question appropriately. – jmcilhinney Sep 16 '20 at 09:53
  • 1
    If you're scaling the Image in your PictureBox, what you draw on its surface needs also to be scaled to match the actual Size of your original Image. See here: [How to draw a transparent shape over an Image](https://stackoverflow.com/a/62724726/7444103), there's a method named `GetScaledSelectionRect()` that might help. – Jimi Sep 16 '20 at 16:09

0 Answers0