【问题标题】:Rectangle Rotation and Flipping矩形旋转和翻转
【发布时间】:2011-08-25 21:03:54
【问题描述】:

我有一个位图,上面绘制了一个 Rectangle 对象。我希望能够 RotateFlip 位图并调整 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.
}

【问题讨论】:

  • 实际代码会增加混乱,但基本目的可以通过通用代码传达。我会把它添加到原始帖子中。
  • 您是仅使用 90 度增量还是需要任意度数?您是围绕矩形的中心点还是其中一个角旋转?与翻转、中心点或边缘相同?

标签: c# bitmap rotation degrees


【解决方案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.
}

【讨论】:

    【解决方案2】:

    我正好遇到了这个问题。

    这是我的结果 - 也许他们会为其他人节省一个小时的摆弄时间......

    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;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-16
      • 1970-01-01
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      相关资源
      最近更新 更多