首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取RotateTransform的结果大小

获取RotateTransform的结果大小
EN

Stack Overflow用户
提问于 2014-01-11 18:24:53
回答 2查看 1.8K关注 0票数 2

我有以下代码,用于在C#中旋转图像:

代码语言:javascript
复制
private Bitmap RotateImage(Bitmap b, float angle)
{
    //create a new empty bitmap to hold rotated image
    Bitmap returnBitmap = new Bitmap(b.Width, b.Height);

    //make a graphics object from the empty bitmap
    Graphics g = Graphics.FromImage(returnBitmap);
    //move rotation point to center of image
    g.TranslateTransform((float)returnBitmap.Width / 2, (float)returnBitmap.Height / 2);

    //rotate
    g.RotateTransform(angle);
    //move image back
    g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
    //draw passed in image onto graphics object
    g.DrawImage(b, new Rectangle(new Point(0, 0), new Size(b.Width, b.Height)));
    return returnBitmap;
}

它的工作非常好,除了它剪辑的结果,当它超过了原来的界限。

据我所知,在旋转后,我必须将返回位图的大小设置为图像的大小。但是,如何找到结果有多大,以相应地设置新位图的大小?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-12 02:22:09

您需要旋转原始图像的四个角,并计算新坐标的包围框:

代码语言:javascript
复制
    private static Bitmap RotateImage(Image b, float angle)
    {
        var corners = new[]
            {new PointF(0, 0), new Point(b.Width, 0), new PointF(0, b.Height), new PointF(b.Width, b.Height)};

        var xc = corners.Select(p => Rotate(p, angle).X);
        var yc = corners.Select(p => Rotate(p, angle).Y);

        //create a new empty bitmap to hold rotated image
        Bitmap returnBitmap = new Bitmap((int)Math.Abs(xc.Max() - xc.Min()), (int)Math.Abs(yc.Max() - yc.Min()));
        ...
    }

    /// <summary>
    /// Rotates a point around the origin (0,0)
    /// </summary>
    private static PointF Rotate(PointF p, float angle)
    {
        // convert from angle to radians
        var theta = Math.PI*angle/180;
        return new PointF(
            (float) (Math.Cos(theta)*(p.X) - Math.Sin(theta)*(p.Y)),
            (float) (Math.Sin(theta)*(p.X) + Math.Cos(theta)*(p.Y)));
    }
票数 3
EN

Stack Overflow用户

发布于 2014-01-11 18:47:05

毕达哥拉斯。它是在90/270角度从原始到平方(w^2+ h^2)的任意位置。我敢打赌,它是由正弦(最大值在90)决定的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21066152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档