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