0

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: 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;
    }
Community
  • 1
  • 1
demilp
  • 43
  • 1
  • 12
  • So what's wrong with your current implementation? It would be helpful to see an example image produced by your code, to get an idea what's missing or going wrong. – Timo Salomäki Jul 28 '16 at 17:39
  • What I have now is Picture A. I need to crop that to achieve Picture B, but with the same proportions than the original one. – demilp Jul 28 '16 at 17:57

0 Answers0