2011-08-25 208 views
0

我有一個位於頂部的矩形對象的位圖。我希望能夠旋轉位圖並調整矩形的x,y,寬度和高度,以便在每次旋轉或翻轉後與位圖對齊。矩形旋轉和翻轉

例如,如果我有一個1000 x 800像素的位圖,我可能會在其上繪製一個帶有指定點和大小的Rectangle對象。

示例代碼:

// A bitmap that's 1000x800 size 
Bitmap bitmap = new Bitmap(fileName); 

// Any arbitrary rectangle that can be drawn inside the bitmap boundaries 
Rectangle rect = new Rectangle(200, 200, 100, 100); 

bitmap.RotateFlip(rotateFlipType); 

switch (rotateFlipType) 
{ 
    case Rotate90FlipNone: 
     // Adjust rectangle to match new bitmap orientation 
     rect = new Rectangle(?, ?, ?, ?); 
     break; 
    case RotateNoneFlip180: 
     rect = new Rectangle(?, ?, ?, ?); 
     break; 
    // ... etc. 
} 
+0

任何現有代碼? – Randy

+0

這裏有什麼問題嗎? –

+0

實際的代碼會增加混淆,但基本目的可以通過泛型代碼傳達。我會將其添加到原始帖子。 –

回答

1

我覺得最簡單的畫圖和標籤rect.Toprect.Bottomrect.Leftrect.Right通過每個場景的理由。一旦完成,我要麼在心理上旋轉圖片,要麼實際旋轉紙張。從那裏,就像找出新的rect.Leftrect.Top住在哪裏一樣簡單。

幾個一般提示:

  • 90度和270度旋轉,rect.Widthrect.Height必須調換。
  • 使用bitmap.Width-rect.Rightbitmap.Height-rect.Bottom來計算新頂部或新左邊通常是最容易的。

這裏是你開始你的兩個例子充滿後可獲取的空白:

switch (rotateFlipType) 
{ 
    case Rotate90FlipNone: 
     // Adjust rectangle to match new bitmap orientation 
     rect = new Rectangle(bitmap.Height-rect.Bottom, 
          rect.Left, 
          rect.Height, 
          rect.Width); 
     break; 
    case RotateNoneFlipHorizontally: 
     rect = new Rectangle(bitmap.Width - rect.Right, 
          rect.Top, 
          rect.Width, 
          rect.Height); 
     break; 
    // ... etc. 
} 
0

我正好面臨這個問題。

這裏是我的結果 - 也許他們會救誰擺弄一小時。

void RotateFlipRect(CRect & pRect, int pNewContainerWidth, int pNewContainerHeight, Gdiplus::RotateFlipType pCorrection) 
{ 
    CRect lTemp = pRect; 

    switch (pCorrection) 
    { 
    case RotateNoneFlipNone: // = Rotate180FlipXY 
     break; 
    case   Rotate90FlipNone: // = Rotate270FlipXY 
     pRect.left = lTemp.top; 
     pRect.top = pNewContainerHeight - lTemp.right; 
     pRect.right = lTemp.bottom; 
     pRect.bottom = pNewContainerHeight - lTemp.left; 
     break; 
    case   Rotate180FlipNone: // = RotateNoneFlipXY 
     pRect.left = pNewContainerWidth - lTemp.right; 
     pRect.top = pNewContainerHeight - lTemp.bottom; 
     pRect.right = pNewContainerWidth - lTemp.left; 
     pRect.bottom = pNewContainerHeight - lTemp.top; 
     break; 
    case   Rotate270FlipNone: // = Rotate90FlipXY 
     pRect.left = pNewContainerWidth - lTemp.bottom; 
     pRect.top = lTemp.left; 
     pRect.right = pNewContainerWidth - lTemp.top; 
     pRect.bottom = lTemp.right; 
     break; 
    case   RotateNoneFlipX: // = Rotate180FlipY 
     pRect.left = pNewContainerWidth - lTemp.right; 
     pRect.top = lTemp.top; 
     pRect.right = pNewContainerWidth - lTemp.left; 
     pRect.bottom = lTemp.bottom; 
     break; 
    case   Rotate90FlipX: // = Rotate270FlipY 
     pRect.left = pNewContainerWidth - lTemp.bottom; 
     pRect.top = pNewContainerHeight - lTemp.right; 
     pRect.right = pNewContainerWidth - lTemp.top; 
     pRect.bottom = pNewContainerHeight - lTemp.left; 
     break; 
    case   Rotate180FlipX: // = RotateNoneFlipY 
     pRect.left = lTemp.left; 
     pRect.top = pNewContainerHeight - lTemp.bottom; 
     pRect.right = lTemp.right; 
     pRect.bottom = pNewContainerHeight - lTemp.top; 
     break; 
    case   Rotate270FlipX: // = Rotate90FlipY 
     pRect.left = lTemp.top; 
     pRect.top = lTemp.left; 
     pRect.right = lTemp.bottom; 
     pRect.bottom = lTemp.right; 
     break; 
    default: 
     // ?!??! 
     break; 
    } 
}