C# 矩形旋转和翻转

C# 矩形旋转和翻转,c#,bitmap,rotation,degrees,C#,Bitmap,Rotation,Degrees,我有一个位图,上面画了一个矩形对象。我希望能够旋转翻转位图,并调整矩形的x、y、宽度和高度,使其在每次旋转或翻转后与位图对齐 例如,如果我有一个1000 x 800像素的位图,我可能会在上面绘制一个具有指定点和大小的矩形对象 示例代码: // A bitmap that's 1000x800 size Bitmap bitmap = new Bitmap(fileName); // Any arbitrary rectangle that can be drawn inside the bi

我有一个位图,上面画了一个矩形对象。我希望能够旋转翻转位图,并调整矩形的x、y、宽度和高度,使其在每次旋转或翻转后与位图对齐

例如,如果我有一个1000 x 800像素的位图,我可能会在上面绘制一个具有指定点和大小的矩形对象

示例代码:

// 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.
}

我发现,通过绘制一张图片并标记
rect.Top
rect.Bottom
rect.Left
rect.Right
可以最容易地对每个场景进行推理。完成后,我要么在心里旋转图片,甚至在身体上旋转纸张。从那里,只需找出新的
rect.Left
rect.Top
的活动位置即可

一些一般提示:

  • 对于90度和270度旋转,必须交换
    rect.Width
    rect.Height
  • 使用
    bitmap.Width rect.Right
    bitmap.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.
}

我面对的正是这个问题

这是我的结果——也许它们能帮别人节省一个小时的时间

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

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