Vielen Dank!!!
funktioniert bei mir nicht ganz...
Bildhöhe wird immer kleiner
Bildabmessungen sollen beibehalten werden
Hier mein Code:
Public Class Bild_drehen
' PictureBox "pb1" W/H: 714/514, SizeMode: Zoom
Dim gBitmap As Drawing.Bitmap
Dim angle As Decimal = Nothing
Private Sub Bild_drehen_Load(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Me.Load
gBitmap = New Bitmap("C:\***\*.JPG") _
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Me.pb1.Width = pb1.Height * (gBitmap.Width / gBitmap.Height)
Me.pb1.Image = gBitmap
End Sub
Private Sub btn_R_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btn_R.Click
angle += 0.5
Me.trbRotate.Value = angle
Me.pb1.Image = Rotate_Bitmap(gBitmap, angle, Color.LightGray)
End Sub
Private Sub btn_0_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btn_0.Click
Me.trbRotate.Value = 0.0
Me.pb1.Image = gBitmap
End Sub
Private Sub btn_L_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btn_L.Click
angle -= 0.5
Me.trbRotate.Value = angle
Me.pb1.Image = Rotate_Bitmap(gBitmap, angle, Color.LightGray)
End Sub
Public Function Rotate_Bitmap(ByVal bmp As Drawing.Bitmap, ByVal angle As _
Decimal, ByVal BackGround As Drawing.Color, Optional ByVal Zoom_Factor As _
Single = 1) As Drawing.Bitmap
Try
Dim rotate_width , rotate_height As Integer
Dim draw_width As Integer = CInt(bmp.Width * Zoom_Factor)
Dim draw_height As Integer = CInt(bmp.Height * Zoom_Factor)
If Not Rotate_Bitmap_Size(draw_width, draw_height, angle, rotate_width, _
rotate_height) Then Return Nothing
Dim bmp_out As New Bitmap(rotate_width, rotate_height)
Dim g As Drawing.Graphics = Graphics.FromImage(bmp_out)
g.Clear(BackGround)
Dim p_center As PointF
p_center.X = CSng(bmp_out.Width / 2): p_center.Y = CSng(bmp_out.Height / 2)
Dim m As New Drawing.Drawing2D.Matrix
m.Scale(Zoom_Factor, Zoom_Factor): m.RotateAt(angle, p_center)
g.Transform = m
Dim source_rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim x As Integer = CInt((rotate_width - draw_width) / 2)
Dim y As Integer = CInt((rotate_height - draw_height) / 2)
Dim destination_rect As New Rectangle(x, y, draw_width, draw_height)
g.DrawImage(bmp, destination_rect, source_rect, GraphicsUnit.Pixel)
m.Dispose
g.Dispose
Return bmp_out
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function Rotate_Bitmap_Size(ByVal width As Integer, ByVal height As _
Integer, ByVal angle As Single, ByRef rotate_width As Integer, ByRef _
rotate_height As Integer) As Boolean
rotate_width = -1 : rotate_height = -1
If width < 1 Or height < 1 Then Return False
'If width > 2048 Or height > 2048 Then Return False
Dim pts(3) As PointF
pts(0).X = 0 : pts(0).Y = 0: pts(1).X = 0 : pts(1).Y = height: pts(2).X = _
width : pts(2).Y = height: pts(3).X = width : pts(3).Y = 0
Dim p_center As Drawing.PointF
p_center.X = CSng(width / 2): p_center.Y = CSng(height / 2)
Dim m As New Drawing.Drawing2D.Matrix
m.RotateAt(angle, p_center)
m.TransformPoints(pts)
m.Dispose()
Dim xug As Single = Single.MaxValue
Dim xog As Single = Single.MinValue
Dim yug As Single = Single.MaxValue
Dim yog As Single = Single.MinValue
For i As Integer = 0 To 3
xug = Math.Min(pts(i).X, xug): xog = Math.Max(pts(i).X, xog): yug = _
Math.Min(pts(i).Y, yug): yog = Math.Max(pts(i).Y, yog)
Next i
'--------------------------------------------
Dim xug2, xog2, yug2, yog2 As Single
xug2 = Single.MaxValue: xog2 = Single.MinValue: yug2 = Single.MaxValue: _
yog2 = Single.MinValue
For i As Integer = 0 To 3
If pts(i).X > xug Then xug2 = Math.Min(pts(i).X, xug2)
If pts(i).X < xog Then xog2 = Math.Max(pts(i).X, xog2)
If pts(i).Y > yug Then yug2 = Math.Min(pts(i).Y, yug2)
If pts(i).Y < yog Then yog2 = Math.Max(pts(i).Y, yog2)
Next i
If xug2 = Single.MaxValue Then xug2 = xug
If xog2 = Single.MinValue Then xog2 = xog
If yug2 = Single.MaxValue Then yug2 = yug
If yog2 = Single.MinValue Then yog2 = yog
'If chkCut.Checked Then
xog = xog2 : yog = yog2
xug = xug2 : yug = yug2
'End If
'--------------------------------------------
rotate_width = CInt(xog - xug)
If rotate_width < 1 Then Return False
rotate_height = CInt(yog - yug)
If rotate_height < 1 Then Return False
Return True
End Function
End Class |