My question is very similar to an already asked question, but in C# I already have the image rotated in the bounding box with the empty spaces, but now I need it cropped and to keep the original proportions.
Here is my desired result:
This is what I have now
public Bitmap RotateImage(Bitmap b, float angle)
{
if (angle > 0)
{
int l = b.Width;
int h = b.Height;
double an = angle * Math.PI / 180;
double cos = Math.Abs(Math.Cos(an));
double sin = Math.Abs(Math.Sin(an));
int nl = (int)(l * cos + h * sin);
int nh = (int)(l * sin + h * cos);
Bitmap returnBitmap = new Bitmap(nl, nh);
Graphics g = Graphics.FromImage(returnBitmap);
g.TranslateTransform((float)(nl - l) / 2, (float)(nh - h) / 2);
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
g.RotateTransform(angle);
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
g.DrawImage(b, new Point(0, 0));
return returnBitmap;
}
else return b;
}