如果我旋转位图并想要跟踪原始图像上翻译到新图像上的点,我将如何实现这一点。我本质上想知道原始图像的右上角在新的旋转和扩展图像上的位置,以便我可以在正确的位置绘制到图形表面。我使用以下代码来旋转位图(最初取自另一篇文章)。
public static Bitmap RotateImage(Bitmap origBmp, float angle)
{
Matrix matrix = new Matrix();
matrix.Translate(origBmp.Width / -2, origBmp.Height / -2, MatrixOrder.Append);
matrix.RotateAt(angle, new Point(0, 0), MatrixOrder.Append);
using (GraphicsPath graphicsPath = new GraphicsPath())
{
// Transform image points by rotation matrix
graphicsPath.AddPolygon(new Point[]
{
new Point(0, 0),
new Point(origBmp.Width, 0),
new Point(0, origBmp.Height)
});
graphicsPath.Transform(matrix);
PointF[] pts = graphicsPath.PathPoints;
Rectangle boundingBox = BoundingBox(origBmp, matrix);
Bitmap bmpDest = new Bitmap(boundingBox.Width, boundingBox.Height);
using (Graphics graphicsDest = Graphics.FromImage(bmpDest))
{
Matrix matrixDest = new Matrix();
matrixDest.Translate(bmpDest.Width / 2, bmpDest.Height / 2, MatrixOrder.Append);
graphicsDest.Transform = matrixDest;
graphicsDest.DrawImage(origBmp, pts);
}
return bmpDest;
}
}
private static Rectangle BoundingBox(
Image img,
Matrix matrix)
{
GraphicsUnit gu = new GraphicsUnit();
Rectangle imgRect = Rectangle.Round(img.GetBounds(ref gu));
// Transform the four points of the image, to get the resized bounding box.
Point topLeft = new Point(imgRect.Left, imgRect.Top);
Point topRight = new Point(imgRect.Right, imgRect.Top);
Point bottomRight = new Point(imgRect.Right, imgRect.Bottom);
Point bottomLeft = new Point(imgRect.Left, imgRect.Bottom);
Point[] points = new Point[]
{
topLeft,
topRight,
bottomRight,
bottomLeft
};
GraphicsPath graphicsPath = new GraphicsPath(
points,
new byte[]
{
(byte)PathPointType.Start,
(byte)PathPointType.Line,
(byte)PathPointType.Line,
(byte)PathPointType.Line
});
graphicsPath.Transform(matrix);
return Rectangle.Round(graphicsPath.GetBounds());
}
具体来说,我正在尝试在正确的位置和旋转处绘制风“倒钩”。
我尝试使用相同的翻译方法来翻译原始图像上的点,但没有成功。
如果我旋转位图并想要跟踪原始位图上的点 在新图像上翻译的图像,我将如何实现这一目标。
/// <summary>
/// Target is starting point; center is pivot; angle is rotation.
/// Returns rotated point (e.g. new cp).
/// </summary>
internal static Point RotatePoint( Point target, Point center, double angle ) {
RotateTransform t = new RotateTransform();
t.CenterX = center.X;
t.CenterY = center.Y;
t.Angle = angle;
Point result = t.TransformPoint( target );
return result;
}