Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a problem with PictureBox scaling. I want to copy a piece of PictureBox1 using a rubberband and show it magnified in PictureBox4.

I can copy a piece but when it shows in PictureBox4 the aspect ratio is not correct.
I would also like to be able to zoom the piece by 2x, 3x etc.

Thank you in advance.
Henrik

Public Class Form1
    ' Rubberband
    Private startCorner As System.Drawing.Point
    Private rubberBand As Rectangle
    Private rubberBanding As Boolean = False
    Private rubberBandColor As Color = Color.Yellow
    Dim MyRect1 As New Rectangle
 
    Private Sub PictureBox1_MouseDown(sender As Object, e As _
      System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            rubberBanding = True
            startCorner = e.Location
            rubberBand = Rectangle.Empty
            PictureBox1.Invalidate()
        End If
    End Sub
 
    Private Sub PictureBox1_MouseMove(sender As Object, e As _
      System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If rubberBanding Then
            rubberBand.Width = Math.Abs(e.X - startCorner.X)
            rubberBand.Height = Math.Abs(e.Y - startCorner.Y)
            rubberBand.X = Math.Min(e.X, startCorner.X)
            rubberBand.Y = Math.Min(e.Y, startCorner.Y)
            PictureBox1.Invalidate()
        End If
    End Sub
 
   Private Sub PictureBox1_MouseUp(sender As Object, e As _
     System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        rubberBanding = False
    End Sub
 
    Private Sub PictureBox1_Paint(sender As Object, e As _
      System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Using pn As New Pen(rubberBandColor, 2) With {.DashStyle = _
        Drawing2D.DashStyle.Dash}
            e.Graphics.DrawRectangle(pn, rubberBand)
            MyRect1 = New Rectangle(rubberBand.X, rubberBand.Y, _
              rubberBand.Width, rubberBand.Height)
        End Using
    End Sub
 
 
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) _
      Handles Button1.Click
        Dim bm As New Bitmap(PictureBox1.Image)
        Dim bmTo As New Bitmap(PictureBox1.Image)
 
        Using g As Graphics = Graphics.FromImage(bmTo)
            Dim to_rect As New Rectangle(0, 0, PictureBox4.Width, _
              PictureBox4.Height)
            If aspect > 1 Then
                to_rect.Height = CInt(to_rect.Height * aspect)
            Else
                to_rect.Width = CInt(to_rect.Width * aspect)
            End If
            g.DrawImage(bm, to_rect, MyRect1, GraphicsUnit.Pixel)
            g.DrawRectangle(Pens.Red, to_rect)
 
            PictureBox4.Image = bmTo
        End Using
    End Sub
End Class
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900