0

enter image description here

How to I rotate an image without it showing like this?

Here's my Rotation Method:

public static Bitmap RotateImageN(Bitmap bmp, float angle)
    {
        Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height);
        using (Graphics g = Graphics.FromImage(rotatedImage))
        {
            // Set the rotation point to the center in the matrix
            g.TranslateTransform(bmp.Width / 2, bmp.Height / 2);
            // Rotate
            g.RotateTransform(angle);
            // Restore rotation point in the matrix
            g.TranslateTransform(-bmp.Width / 2, -bmp.Height / 2);
            // Draw the image on the bitmap
            g.DrawImage(bmp, new Point(0, 0));
        }

        return rotatedImage;
    }

Edit: After trying Loocid's Code

enter image description here

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
TerribleDog
  • 1,237
  • 1
  • 8
  • 31

2 Answers2

3

Your rotatedImage Bitmap needs to be big enough to accommodate the rotated image.

Say you rotated your original image by 30° you need to get the size of the bounding box like so:

enter image description here

Using some basic trig:

x = L*cos(30 * π / 180) + w*cos(60 * π / 180)

y = L*sin(30 * π / 180) + w*sin(60 * π / 180)

Therefore change the start of your code to:

var x = bmp.Width * Math.Cos(angle * Math.PI / 180) + bmp.Height * Math.Cos((90-angle) * Math.PI / 180)
var y = bmp.Width * Math.Sin(angle * Math.PI / 180) + bmp.Height * Math.Sin((90-angle) * Math.PI / 180)
Bitmap rotatedImage = new Bitmap(x, y);
Loocid
  • 6,112
  • 1
  • 24
  • 42
1

The issue occurs in the rotating is related to the bounding box. It is clipping the edge because of the image you provided does not fit into the area that you have given.

I also faced this issue. So I tried a solution from here.

Adding the code that works for me.

public static Bitmap RotateImageN(Bitmap bitmap, float angle)
{
    Matrix matrix = new Matrix();
    matrix.Translate(bitmap.Width / -2, bitmap.Height / -2, MatrixOrder.Append);
    matrix.RotateAt(angle, new System.Drawing.Point(0, 0), MatrixOrder.Append);
    using (GraphicsPath graphicsPath = new GraphicsPath())
    {
        graphicsPath.AddPolygon(new System.Drawing.Point[] { new System.Drawing.Point(0, 0), new System.Drawing.Point(bitmap.Width, 0), new System.Drawing.Point(0, bitmap.Height) });
        graphicsPath.Transform(matrix);
        System.Drawing.PointF[] points = graphicsPath.PathPoints;

        Rectangle rectangle = boundingBox(bitmap, matrix);
        Bitmap resultBitmap = new Bitmap(rectangle.Width, rectangle.Height);

        using (Graphics gDest = Graphics.FromImage(resultBitmap))
        {
            Matrix mDest = new Matrix();
            mDest.Translate(resultBitmap.Width / 2, resultBitmap.Height / 2, MatrixOrder.Append);
            gDest.Transform = mDest;
            gDest.DrawImage(bitmap, points);
            return resultBitmap;
        }
    }
}

private static Rectangle boundingBox(Image image, Matrix matrix)
{
    GraphicsUnit graphicsUnit = new GraphicsUnit();
    Rectangle boundingRectangle = Rectangle.Round(image.GetBounds(ref graphicsUnit));
    Point topLeft = new Point(boundingRectangle.Left, boundingRectangle.Top);
    Point topRight = new Point(boundingRectangle.Right, boundingRectangle.Top);
    Point bottomRight = new Point(boundingRectangle.Right, boundingRectangle.Bottom);
    Point bottomLeft = new Point(boundingRectangle.Left, boundingRectangle.Bottom);
    Point[] points = new Point[] { topLeft, topRight, bottomRight, bottomLeft };
    GraphicsPath graphicsPath = new GraphicsPath(points, new byte[] { (byte)PathPointType.Start, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line });
    graphicsPath.Transform(matrix);
    return Rectangle.Round(graphicsPath.GetBounds());
}
Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17