2013-07-11 49 views
1

下面是一張圖片,說明我正在嘗試做什麼。找到旋轉後的圖像內的原始點c#

http://tinypic.com/r/nvbehh/5

我旋轉在c#的圖像(白色矩形)。 問題是我需要將綠色正方形(另一個圖像)放在與矩形相同的點上,而不管旋轉。綠色正方形不能與矩形一起旋轉。

正如你可以看到在白色矩形旋轉後,我每次都會得到不同大小的畫布。

我想找到確切的點來放置新的旋轉圖像創建後的綠色廣場。

以下OK是一些代碼,它旋轉圖像並將點放在正確的位置。 現在的問題是,如果我在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; 

    } 
} 

所以現在我需要能夠取消對TranslateTransform在RotateImage方法,讓我看到了正確的矩形和修改RotatePoint方法,所以我得到正確的新位置

+0

你需要做數學題,即旋轉相匹配。也許谷歌「旋轉矩形算法」。您正在尋找給定4個角的XY位置的數學,應用旋轉。我強調你正在尋找MATH,而不是調用GUI。使用Sin()和Cos()的東西。那麼你需要弄清楚這與你的視覺形象有何關係。 – ToolmakerSteve

+0

OK上面確實打算。 @ToolmakerSteve謝謝你指點我正確的方向。不知道如何標記爲答案等 – Ray

+0

很高興這有幫助。你可以爲自己的問題寫一個答案(「回答你的問題」按鈕),並鼓勵他們這樣做,這樣人們就知道它已經得到了回答。然後將其標記爲答案。如果您在問題中已經更正了這個問題,那麼您可能會在回答中顯示「之前」和「之後」,以表明您最初失蹤的內容。 – ToolmakerSteve

回答

0

這裏是工作控制器。
公共類IconController:控制器 {// // GET:/圖標/

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; 

    } 
}