2011-08-25 210 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; 
    } 
}