矩形旋转和翻转

时间:2011-08-25 21:03:55

标签: c# bitmap rotation degrees

我有一个位于其顶部的Rectangle对象的位图。我想能够旋转位图并调整矩形的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.
}

2 个答案:

答案 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.
}

答案 1 :(得分: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;
  }
}