根据两个点旋转图像

7

我的表单上有两个点和一个 picturebox,如下所示:

*

   [^]
   [ ]

          *

我想要把图片框与点对齐,使其看起来像这样:
*

   \^\
    \ \

          *

我要如何计算角度以及如何旋转PictureBox?

目前我正在使用以下代码:

double xDifference = Math.Abs(point2.X - point1.X);
double yDifference = Math.Abs(point2.Y - point1.Y);

double angle = Math.Atan(yDifference / xDifference) * 180 / Math.PI;

但这样做不行,因为x和y的值是绝对的,如果点2在点1的左边,它们无法计算。

为了旋转图像,我找到了以下函数:

    public Bitmap rotateImage(Image image, PointF offset, float angle) {
        // Create a new empty bitmap to hold rotated image
        Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
        rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        // Make a graphics object from the empty bitmap
        Graphics g = Graphics.FromImage(rotatedBmp);

        // Put the rotation point in the center of the image
        g.TranslateTransform(offset.X, offset.Y);

        // Rotate the image
        g.RotateTransform(angle);

        // Move the image back
        g.TranslateTransform(-offset.X, -offset.Y);

        // Draw passed in image onto graphics object
        g.DrawImage(image, new PointF(0, 0));

        return rotatedBmp;
    }

我该如何使用该函数?我不确定在偏移量中插入什么值。谢谢。

2
我喜欢这个“图片”。 - PiotrWolkowski
2
首先,你不应该使用 abs,而应该使用 Math.Atan2 - Vlad
第二点,您想要围绕哪个点旋转?如果是围绕图像中心旋转,您的偏移量应该为(-image.Width/2, -image.Height/2)。 - Vlad
@Vlad 你有关于计算picturebox新宽度和高度的建议吗? - Qub1
@Qub1:嗯,你可以精确计算它。 新宽度 = 旧宽度 * abs(cos(angle)) + 旧高度 * abs(sin(angle))新高度 = 旧宽度 * abs(sin(angle)) + 旧高度 * abs(cos(angle)) - Vlad
显示剩余2条评论
2个回答

3
我不喜欢在不必要的情况下使用角度。
在这里,你只想改变正交标准基。
从 {X;Y} 到 {U;V},其中 V (单位长度) 平行于 AB (或 point1 point2)。
因为 {U;V} 是一个正交标准基,所以 Ux = Vy 和 Uy = -Vx。
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace CsiChart
{
    public partial class CustomControl1 : Control
    {
        private const float EPSILON = 1e-6f;

        private Image _image;
        private ImageLayout _imageLayout = ImageLayout.Center;
        private PointF _pointA = new PointF(0, 100);
        private PointF _pointB = new PointF(100, 0);

        public CustomControl1()
        {
            InitializeComponent();
        }

        public Image Image
        {
            get { return _image; }
            set
            {
                if (Equals(_image, value)) return;
                _image = value;
                Invalidate();
                OnImageChanged(EventArgs.Empty);
            }
        }

        public event EventHandler ImageChanged;

        public ImageLayout ImageLayout
        {
            get { return _imageLayout; }
            set
            {
                if (Equals(_imageLayout, value)) return;
                _imageLayout = value;
                Invalidate();
                OnImageLayoutChanged(EventArgs.Empty);
            }
        }

        public event EventHandler ImageLayoutChanged;

        public PointF PointA
        {
            get { return _pointA; }
            set
            {
                if (Equals(_pointA, value)) return;
                _pointA = value;
                Invalidate();
                OnPointAChanged(EventArgs.Empty);
            }
        }

        public event EventHandler PointAChanged;

        public PointF PointB
        {
            get { return _pointB; }
            set
            {
                if (Equals(_pointB, value)) return;
                _pointB = value;
                Invalidate();
                OnPointBChanged(EventArgs.Empty);
            }
        }

        public event EventHandler PointBChanged;

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            if (DesignMode) return;

            var g = pe.Graphics;
            g.Clear(BackColor);         

            var image = Image;
            if (image == null) return;

            var clientRectangle = ClientRectangle;
            var centerX = clientRectangle.X + clientRectangle.Width / 2;
            var centerY = clientRectangle.Y + clientRectangle.Height / 2;

            var srcRect = new Rectangle(new Point(0, 0), image.Size);

            var pointA = PointA;
            var pointB = PointB;

            // Compute U, AB vector normalized.
            var vx = pointB.X - pointA.X;
            var vy = pointB.Y - pointA.Y;
            var vLength = (float) Math.Sqrt(vx*vx + vy*vy);
            if (vLength < EPSILON)
            {
                vx = 0;
                vy = 1;
            }
            else
            {
                vx /= vLength;
                vy /= vLength;
            }

            var oldTransform = g.Transform;

            // Change basis to U,V
            // We also take into acount the inverted on screen Y.
            g.Transform = new Matrix(-vy, vx, -vx, -vy, centerX, centerY);

            var imageWidth = image.Width;
            var imageHeight = image.Height;

            RectangleF destRect;
            switch (ImageLayout)
            {
                case ImageLayout.None:
                    destRect = new Rectangle(0, 0, imageWidth, imageHeight);
                    break;
                case ImageLayout.Center:
                    destRect = new Rectangle(-imageWidth/2, -imageHeight/2, imageWidth, imageHeight);
                    break;
                case ImageLayout.Zoom:
                    // XY aligned bounds size of the image.
                    var imageXSize = imageWidth*Math.Abs(vy) + imageHeight*Math.Abs(vx);
                    var imageYSize = imageWidth*Math.Abs(vx) + imageHeight*Math.Abs(vy);

                    // Get best scale to fit.
                    var s = Math.Min(clientRectangle.Width/imageXSize, clientRectangle.Height/imageYSize);
                    destRect = new RectangleF(-imageWidth*s/2, -imageHeight*s/2, imageWidth*s, imageHeight*s);
                    break;
                default:
                    throw new InvalidOperationException();
            }

            g.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
            g.Transform = oldTransform;
        }

        protected virtual void OnImageChanged(EventArgs eventArgs)
        {
            var handler = ImageChanged;
            if (handler == null) return;
            handler(this, eventArgs);
        }

        protected virtual void OnImageLayoutChanged(EventArgs eventArgs)
        {
            var handler = ImageLayoutChanged;
            if (handler == null) return;
            handler(this, eventArgs);
        }

        private void OnPointAChanged(EventArgs eventArgs)
        {
            var handler = PointAChanged;
            if (handler == null) return;
            handler(this, eventArgs);
        }

        private void OnPointBChanged(EventArgs eventArgs)
        {
            var handler = PointBChanged;
            if (handler == null) return;
            handler(this, eventArgs);
        }
    }
}

2

让我们把所有的计算放在一起。

首先,连接两点的直线方向可以通过以下方式计算:

double xDifference = point2.X - point1.X;
double yDifference = point2.Y - point1.Y;

double angleRadians = Math.Atan2(yDifference, xDifference);

接下来,垂直方向(90度)必须与旋转后考虑的方向平行,因此旋转角度为

double rotationAngleRadians = angleDegrees - Math.PI/2;

有了这个角度,我们可以计算出边界框的大小:
double newWidth =  image.Width * Math.Abs(Math.Cos(rotationAngleRadians)) +
                   image.Height * Math.Abs(Math.Sin(rotationAngleRadians));
double newHeight = image.Width * Math.Abs(Math.Sin(rotationAngleRadians)) +
                   image.Height * Math.Abs(Math.Cos(rotationAngleRadians));

现在,我们需要先进行转换,使旧图像的中心位于位置0。这使得平移变换为(-image.Width/2, -image.Height/2)。然后,我们应用旋转角度rotationAngleDegrees(它是rotationAngleRadians * 180 / Math.PI),因为Graphics期望角度以度为单位。然后,我们将图像移动到图像的中间,即平移变换为(newWidth/2, newHeight/2)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接