How do I prevent clipping when rotating an image in C#?

后端 未结 4 1849
北荒
北荒 2020-12-06 15:19

I just went through a bunch of stuff trying to figure out how to get the image to even rotate. That works but now it\'s clipping and I\'m not really sure how to make it stop

4条回答
  •  感情败类
    2020-12-06 15:42

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

提交回复
热议问题