Meine Rotationsroutine:
Private Sub Rotate()
Dim relPicIn, RelPic2 As Decimal
Dim inW_half, inH_half As Decimal
Dim rotW_half, rotH_half, rotL, rotT As Decimal
Dim theta As Double = HScrollBar1.Value * PI / 180.0
If theta < 0 Then theta *= -1 theta * -1
Dim sin_theta As Double = Sin(theta)
Dim cos_theta As Double = Cos(theta)
inW_half = CDec(bmp_In.Width/2)
inH_half = CDec(bmp_In.Height/2)
rotW_half = CDec(Math.Floor(inW_half*cos_theta+inH_half*sin_theta))
rotH_half = CDec(Math.Floor(inW_half*sin_theta+inH_half*cos_theta))
rotL = CDec(rotW_half-inW_half*cos_theta+inH_half*sin_theta)
rotT = CDec(rotH_half-(-inW_half*sin_theta+inH_half*cos_theta))
bmp_Out = New Bitmap(CInt(2*rotW_half), CInt(2*rotH_half))
Dim g As Drawing.Graphics = Graphics.FromImage(bmp_Out)
g.Clear(Color.LightGray)
Dim p_center As PointF
p_center.X = rotW_half
p_center.Y = rotH_half
Dim m As New Drawing.Drawing2D.Matrix
m.RotateAt(HScrollBar1.Value, p_center)
g.Transform = m
Dim source_rect As New Rectangle(0, 0, bmp_In.Width, bmp_In.Height)
Dim intX As Integer = CInt((bmp_Out.Width-bmp_In.Width)/2)
Dim intY As Integer = CInt((bmp_Out.Height-bmp_In.Height)/2)
Dim dest_rect As New Rectangle(intX,intY,bmp_In.Width,bmp_In.Height)
g.DrawImage(bmp_In, dest_rect, source_rect, GraphicsUnit.Pixel)
m.Dispose()
g.Dispose()
pb1.Image = bmp_Out
RelPic2 = rotT/(2*rotW_half-rotL)
relPicIn = CDec(bmp_In.Height/bmp_In.Width)
If relPicIn < 1 Then
If relPicIn = pb1.Height/pb1.Width Then
out_rect.Width = CInt(Math.Floor(2*(rotH_half-rotT+rotW_half*RelPic2)/( _
relPicIn+RelPic2)/16)*16)
Else
out_rect.Width = CInt(Math.Floor(2*(rotH_half-rotT+rotW_half*RelPic2)/( _
relPicIn+RelPic2)))
End |
out_rect.Height = CInt(Math.Floor(out_rect.Width*relPicIn))
Else
out_rect.Height = CInt((2*(rotW_half-rotL+rotH_half*RelPic2)/( _
bmp_In.Width/bmp_In.Height+RelPic2)))
out_rect.Width = CInt((out_rect.Height/relPicIn))
End If
out_rect.X = CInt(rotW_half-out_rect.Width/2)
out_rect.y = CInt(rotH_half-out_rect.Height/2)
If bmp_Out.Height/bmp_Out.Width > pb1.Height/pb1.Width Then
decZoom = CDec(bmp_Out.Height/pb1.Height)
Else
decZoom = CDec(bmp_Out.Width/pb1.Width)
End If
If pb1.SizeMode = PictureBoxSizeMode.CenterImage Then decZoom = 1
Dim ImagePart As Bitmap = New Bitmap(out_rect.Width, out_rect.Height)
g = Graphics.FromImage(ImagePart)
Dim TargetRect As Rectangle = New Rectangle(0, 0, out_rect.Width, _
out_rect.Height)
g.DrawImage(bmp_Out, TargetRect, out_rect, GraphicsUnit.Pixel)
g.Dispose
g = Nothing
pb2.Image = ImagePart
If chbVollbild.Checked = True Then pb1.Image = ImagePart
End Sub |