다음은 수행하려는 작업을 보여주는 그림입니다.회전 후 이미지 내에서 원점 찾기 C#
나는 C#에서 이미지 (흰색 사각형)를 회전하고있다. 문제는 회전에 관계없이 직사각형을 기준으로 동일한 지점에 녹색 사각형 (다른 이미지)을 배치해야한다는 것입니다. 녹색 사각형은 사각형과 함께 회전 할 수 없습니다.
흰색 직사각형의 회전 후에 볼 수 있듯이 매번 다른 크기의 캔버스로 끝납니다.
새로운 회전 이미지를 만든 후 녹색 사각형을 놓을 정확한 위치를 찾으려고합니다.
다음은 이미지를 회전시키고 올바른 위치에 도트를 넣는 코드입니다. 이제 RotateImage 메서드에서 변형을 적용하면 모든 rectagle이 표시되지만 빨간색 점이 잘못된 지점에 있음을 알 수 있습니다. 나는 점의 포인트를 알 필요가 있다는 것을 강조해야한다.
public class IconController : Controller
{
//
// GET: /Icon/
public ActionResult Index()
{
return View();
}
////Icon/Icon?connected=true&heading=320&type=logo45
public ActionResult Icon(bool connected, float heading, string type)
{
var dir = Server.MapPath("/images");
//RED SQUARE IM TRYING TO PLACE ON THE BLUE RECTANGLE.
var path = Path.Combine(dir, "mapicons/center.png");
//GREEN RECTANGLE WITH FIXED YELLOW (Actual center) AND BLUE (point im really trying to find)
var path2 = Path.Combine(dir, "mapicons/connected-marker.png");
Image innerIcon = Image.FromFile(path);
Image marker = Image.FromFile(path2);
using (marker)
{
Point orginalCenter = new Point((marker.Width/2), (marker.Height/2));
Bitmap markerbitmap = RotateImage(new Bitmap(marker), heading);
marker = (Image)markerbitmap;
using (var bitmap = new Bitmap(marker.Width, marker.Height))
{
using (var canvas = Graphics.FromImage(bitmap))
{
PointF newCenter = RotatePoint(orginalCenter, 80, 120, heading, marker.Width, marker.Height);
canvas.DrawRectangle(new Pen(Color.Black), 0, 0, bitmap.Width, bitmap.Height);
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.DrawImage(marker, new Rectangle(0, 0, marker.Width, marker.Height), new Rectangle(0, 0, marker.Width, marker.Height), GraphicsUnit.Pixel);
canvas.DrawImage(innerIcon, newCenter.X - (innerIcon.Width/2), newCenter.Y - (innerIcon.Height/2));
canvas.Save();
}
try
{
bitmap.Save(Path.Combine(dir, "result.png"), ImageFormat.Png);
path = Path.Combine(dir, "result.png");
}
catch (Exception ex) { }
}
}
return base.File(path, "image/png");
}
public static Bitmap RotateImage(Bitmap b, float Angle)
{
// The original bitmap needs to be drawn onto a new bitmap which will probably be bigger
// because the corners of the original will move outside the original rectangle.
// An easy way (OK slightly 'brute force') is to calculate the new bounding box is to calculate the positions of the
// corners after rotation and get the difference between the maximum and minimum x and y coordinates.
float wOver2 = b.Width/2.0F;
float hOver2 = b.Height/2.0F;
float radians = -(float)(Angle/180.0 * Math.PI);
// Get the coordinates of the corners, taking the origin to be the centre of the bitmap.
PointF[] corners = new PointF[]{
new PointF(-wOver2, -hOver2),
new PointF(+wOver2, -hOver2),
new PointF(+wOver2, +hOver2),
new PointF(-wOver2, +hOver2)
};
for (int i = 0; i < 4; i++)
{
PointF p = corners[i];
PointF newP = new PointF((float)(p.X * Math.Cos(radians) - p.Y * Math.Sin(radians)), (float)(p.X * Math.Sin(radians) + p.Y * Math.Cos(radians)));
corners[i] = newP;
}
// Find the min and max x and y coordinates.
float minX = corners[0].X;
float maxX = minX;
float minY = corners[0].Y;
float maxY = minY;
for (int i = 1; i < 4; i++)
{
PointF p = corners[i];
minX = Math.Min(minX, p.X);
maxX = Math.Max(maxX, p.X);
minY = Math.Min(minY, p.Y);
maxY = Math.Max(maxY, p.Y);
}
// Get the size of the new bitmap.
SizeF newSize = new SizeF(maxX - minX, maxY - minY);
// ...and create it.
Bitmap returnBitmap = new Bitmap((int)Math.Ceiling(newSize.Width), (int)Math.Ceiling(newSize.Height));
// Now draw the old bitmap on it.
using (Graphics g = Graphics.FromImage(returnBitmap))
{
g.TranslateTransform(newSize.Width/2.0f, newSize.Height/2.0f);
g.RotateTransform(Angle);
g.TranslateTransform(-b.Width/2.0f, -b.Height/2.0f);
g.DrawImage(b, 0, 0);
}
return returnBitmap;
}
public static Point RotatePoint(Point pointToRotate, Point centerPoint, double angleInDegrees)
{
double angleInRadians = angleInDegrees * (Math.PI/180);
double cosTheta = Math.Cos(angleInRadians);
double sinTheta = Math.Sin(angleInRadians);
Point pt = new Point();
pt.X = (int)(cosTheta * (pointToRotate.X-centerPoint.X) - sinTheta * (pointToRotate.Y-centerPoint.Y) + centerPoint.X);
pt.Y = (int)(sinTheta * (pointToRotate.X - centerPoint.X) + cosTheta * (pointToRotate.Y - centerPoint.Y) + centerPoint.Y);
//p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy
return pt;
}
}
그래서 지금은 내가 올바른 사각형을 볼 수 있도록 RotateImage 방법에 TranslateTransform의 주석을 해제 할 수 있어야하고, 내가 올바른 새 위치 여기
당신은 회전과 일치, 수학을해야합니다. 어쩌면 구글은 "회전 알고리즘을 회전". 당신은 4 모서리의 XY 위치를 주어진 수학을 찾고 회전을 적용합니다. GUI에 대한 호출이 아니라 MATH를 찾고 있다고 강조했습니다. Sin()과 Cos()를 사용하는 것. 그런 다음 이것이 당신의 시각적 이미지와 어떤 관련이 있는지 알아야합니다. – ToolmakerSteve
OK 위의 내용은 현재 의도 한 것입니다. @ 도구 메이커 스티브는 올바른 방향으로 나를 가리켜 주셔서 감사합니다. 답변으로 표시하는 방법을 잘 모르겠습니다. – Ray
도움이 되니 기쁩니다. 자신의 질문에 대한 답변을 작성할 수 있습니다 ("Answer Your Question"버튼). 그런 다음 답으로 표시하십시오. 질문에서 이미 문제를 해결했다면, 답안에 "이전"과 "후"를 표시하여 처음에 누락 된 내용을 나타낼 수 있습니다. – ToolmakerSteve