How can I rotate an image by any degree?

Based on the previous answers I created this code that doesn't cut the image (the other examples were not working for me)

    private Bitmap RotateImage(Bitmap bmp, float angle)
    {
        float height = bmp.Height;
        float width = bmp.Width;
        int hypotenuse = System.Convert.ToInt32(System.Math.Floor(Math.Sqrt(height * height + width * width)));
        Bitmap rotatedImage = new Bitmap(hypotenuse, hypotenuse);
        using (Graphics g = Graphics.FromImage(rotatedImage))
        {
            g.TranslateTransform((float)rotatedImage.Width / 2, (float)rotatedImage.Height / 2); //set the rotation point as the center into the matrix
            g.RotateTransform(angle); //rotate
            g.TranslateTransform(-(float)rotatedImage.Width / 2, -(float)rotatedImage.Height / 2); //restore rotation point into the matrix
            g.DrawImage(bmp, (hypotenuse - width) / 2, (hypotenuse - height) / 2, width, height);
        }
        return rotatedImage;
    }

I tried the answer of @Omar myself and realized, that the original image gets cut at the sides ... i've rewritten it so it resizes the image to the new sizes:

private static Bitmap RotateImage(Bitmap bmp, float angle)
{
    float alpha = angle;

    //edit: negative angle +360
    while(alpha <0) alpha +=360;

    float gamma = 90;
    float beta = 180 - angle - gamma;

    float c1 = bmp.Height;
    float a1 = (float)(c1 * Math.Sin(alpha * Math.PI / 180) / Math.Sin(gamma * Math.PI / 180));
    float b1 = (float)(c1 * Math.Sin(beta * Math.PI / 180) / Math.Sin(gamma * Math.PI / 180));

    float c2 = bmp.Width;
    float a2 = (float)(c2 * Math.Sin(alpha * Math.PI / 180) / Math.Sin(gamma * Math.PI / 180));
    float b2 = (float)(c2 * Math.Sin(beta * Math.PI / 180) / Math.Sin(gamma * Math.PI / 180));

    int width = Convert.ToInt32(b2 + a1);
    int height = Convert.ToInt32(b1 + a2);

    Bitmap rotatedImage = new Bitmap(width, height);
    using (Graphics g = Graphics.FromImage(rotatedImage))
    {
        g.TranslateTransform(rotatedImage.Width / 2, rotatedImage.Height / 2); //set the rotation point as the center into the matrix
        g.RotateTransform(angle); //rotate
        g.TranslateTransform(-rotatedImage.Width / 2, -rotatedImage.Height / 2); //restore rotation point into the matrix
        g.DrawImage(bmp, new Point((width - bmp.Width) / 2, (height - bmp.Height) / 2)); //draw the image on the new bitmap
    }
    return rotatedImage;
}

Changes of the Size at 30 Degree Radius


I didn't understand what's your problem but I think that your code could be improved. I think that you don't need to use directly the Matrix class. There are some functions that does this work for you. Infact the only things you need are: set the point of the rotation as the center, rotate the graphics and draw on it, using some functions by the Graphics class. So to rotate an image you can use this simple code:

private Bitmap RotateImage(Bitmap bmp, float angle) {
     Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height);
     rotatedImage.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);

     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;
}

Tags:

C#