Hallo,
ich habe eine Bitmap und möchte dieses um einen beliebigen Winkel drehen. Danach möchte ich die neue Größe ermitteln und das neue Bitmap anzeigen lassen.
Folgenden Code habe ich zusammengestellt:
Das Drehen funktioniert. Allerdings ist noch ein Fehler darin, weil das neue Bild abgeschnittene Ecken hat.
Hat jemand von euch eine Idee, was ich übersehen bzw. falsch gemacht habe?
Vielen Dank schon mal im Voraus
ich habe eine Bitmap und möchte dieses um einen beliebigen Winkel drehen. Danach möchte ich die neue Größe ermitteln und das neue Bitmap anzeigen lassen.
Folgenden Code habe ich zusammengestellt:
VB.NET-Quellcode
- Public Sub BildDrehen(Ursprungsbild As Bitmap, Winkel As Integer, Zielbild As Bitmap)
- Dim Grafikobjekt As Graphics = Graphics.FromImage(Zielbild)
- Dim Eckpunkte(3) As Point
- Dim Bildmatrix As New Drawing2D.Matrix
- Dim Breite As Integer = 1
- Dim Hoehe As Integer = 1
- Dim XMin As Single = Ursprungsbild.Width * Ursprungsbild.Height * 2
- Dim XMax As Single = -XMin
- Dim YMin As Single = XMin
- Dim YMax As Single = XMax
- Dim Zaehler As Integer
- Dim Mittelpunkt As Drawing.PointF
- Mittelpunkt.X = CSng(Ursprungsbild.Width / 2)
- Mittelpunkt.Y = CSng(Ursprungsbild.Height / 2)
- Eckpunkte(0).X = 0
- Eckpunkte(0).Y = 0
- Eckpunkte(1).X = Ursprungsbild.Width - 1
- Eckpunkte(1).Y = 0
- Eckpunkte(2).X = Ursprungsbild.Width - 1
- Eckpunkte(2).Y = Ursprungsbild.Height - 1
- Eckpunkte(3).X = 0
- Eckpunkte(3).Y = Ursprungsbild.Height - 1
- Bildmatrix.RotateAt(Winkel, Mittelpunkt)
- Bildmatrix.TransformPoints(Eckpunkte)
- For Zaehler = 0 To 3
- XMin = System.Math.Min(Eckpunkte(Zaehler).X, XMin)
- XMax = System.Math.Max(Eckpunkte(Zaehler).X, XMax)
- YMin = System.Math.Min(Eckpunkte(Zaehler).Y, YMin)
- YMax = System.Math.Max(Eckpunkte(Zaehler).Y, YMax)
- Next
- Breite = CInt(XMax - XMin)
- Hoehe = CInt(YMax - YMin)
- Zielbild = New Bitmap(Breite, Hoehe)
- Grafikobjekt = Graphics.FromImage(Zielbild)
- Grafikobjekt.TranslateTransform(CInt(Zielbild.Width / 2), CInt(Zielbild.Height / 2))
- Grafikobjekt.RotateTransform(Winkel)
- Grafikobjekt.TranslateTransform(-CInt(Zielbild.Width / 2), -CInt(Zielbild.Height / 2))
- Grafikobjekt.DrawImage(Ursprungsbild, CInt((Breite - Ursprungsbild.Width) / 2), CInt((Hoehe - Ursprungsbild.Height) / 2), Breite, Hoehe)
- Dim S As Graphics = Graphics.FromImage(SichtfensterPicture.Image)
- S.DrawImage(Zielbild, New Rectangle(0, 0, Breite, Hoehe), New Rectangle(0, 0, Breite, Hoehe), GraphicsUnit.Pixel)
- End Sub
Das Drehen funktioniert. Allerdings ist noch ein Fehler darin, weil das neue Bild abgeschnittene Ecken hat.
Hat jemand von euch eine Idee, was ich übersehen bzw. falsch gemacht habe?
Vielen Dank schon mal im Voraus