public static Bitmap RotateBitmap_NoPadding(Bitmap image, double degrees, Color?bgColor) { PointF[] points = new PointF[] { new PointF(0, 0), new PointF(image.Width, 0), new PointF(0, image.Height), new PointF(image.Width, image.Height), }; double angle = PolarPoint.ToRadians(degrees); float minX = int.MaxValue, minY = int.MaxValue, maxX = int.MinValue, maxY = int.MinValue; for (int i = 0; i < points.Length; i++) { PolarPoint p = new PolarPoint(points[i]); p.Angle += angle; points[i] = p.ToPointF(); minX = Math.Min(minX, points[i].X); minY = Math.Min(minY, points[i].Y); maxX = Math.Max(maxX, points[i].X); maxY = Math.Max(maxY, points[i].Y); } Size size = Size.Ceiling(new SizeF(maxX - minX, maxY - minY)); Bitmap target = new Bitmap(size.Width, size.Height); using (Graphics g = Graphics.FromImage(target)) { g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; if (bgColor != null && bgColor != Color.Transparent) { using (Brush b = new SolidBrush(bgColor.Value)) g.FillRectangle(b, 0, 0, size.Width, size.Height); } g.TranslateTransform(-minX, -minY); g.DrawImage(image, new PointF[] { points[0], points[1], points[2] }); } return(target); }
public static Bitmap RotateBitmap_NoPadding(Bitmap image, double degrees, Color? bgColor) { PointF[] points = new PointF[] { new PointF(0, 0), new PointF(image.Width, 0), new PointF(0, image.Height), new PointF(image.Width, image.Height), }; double angle = PolarPoint.ToRadians(degrees); float minX = int.MaxValue, minY = int.MaxValue, maxX = int.MinValue, maxY = int.MinValue; for (int i = 0; i < points.Length; i++) { PolarPoint p = new PolarPoint(points[i]); p.Angle += angle; points[i] = p.ToPointF(); minX = Math.Min(minX, points[i].X); minY = Math.Min(minY, points[i].Y); maxX = Math.Max(maxX, points[i].X); maxY = Math.Max(maxY, points[i].Y); } Size size = Size.Ceiling(new SizeF(maxX - minX, maxY - minY)); Bitmap target = new Bitmap(size.Width, size.Height); using (Graphics g = Graphics.FromImage(target)) { g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; if (bgColor != null && bgColor != Color.Transparent) { using (Brush b = new SolidBrush(bgColor.Value)) g.FillRectangle(b, 0, 0, size.Width, size.Height); } g.TranslateTransform(-minX, -minY); g.DrawImage(image, new PointF[] { points[0], points[1], points[2] }); } return target; }