2012-04-22 2 views
0

를 회전하는 방법 이미지를 회전하여 문제를 해결하기 위해 필요하지만, 나는 완전히PictureBox를

public static Image RotateImage(Image img, float rotationAngle) 
     { 
      //create an empty Bitmap image 
      Bitmap bmp = new Bitmap(img.Width, img.Height); 

      //turn the Bitmap into a Graphics object 
      Graphics gfx = Graphics.FromImage(bmp); 

      //now we set the rotation point to the center of our image 
      gfx.TranslateTransform((float)bmp.Width/2, (float)bmp.Height/2); 

      //now rotate the image 
      gfx.RotateTransform(rotationAngle); 

      gfx.TranslateTransform(-(float)bmp.Width/2, -(float)bmp.Height/2); 

      //set the InterpolationMode to HighQualityBicubic so to ensure a high 
      //quality image once it is transformed to the specified size 
      gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; 

      //now draw our new image onto the graphics object 
      gfx.DrawImage(img, new System.Drawing.Point(0, 0)); 

      //dispose of our Graphics object 
      gfx.Dispose(); 

      //return the image 
      return bmp; 
     } 

을 돌려 메서드를 호출하려면이 옵션을 사용하지 않는 이미지를 회전하려면이 코드가 있습니다. 권리는 포함 이미지 사각형을 회전, 이미지

Bitmap bitmap = (Bitmap)Pix.Image; 
Pix.Image = (Bitmap)(RotateImage(bitmap, 20.0f)); 
+0

중복 가능성 [LockBits 이미지 회전 방법은 작동하지? (http://stackoverflow.com/questions/3860030/lockbits-image-rotation-method-not-working) –

답변

1
private Bitmap RotateImageByAngle(Image oldBitmap, float angle) 
{ 
    var newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height); 
    newBitmap.SetResolution(oldBitmap.HorizontalResolution, oldBitmap.VerticalResolution); 
    var graphics = Graphics.FromImage(newBitmap); 
    graphics.TranslateTransform((float)oldBitmap.Width/2, (float)oldBitmap.Height/2); 
    graphics.RotateTransform(angle); 
    graphics.TranslateTransform(-(float)oldBitmap.Width/2, -(float)oldBitmap.Height/2); 
    graphics.DrawImage(oldBitmap, new Point(0, 0)); 
    return newBitmap; 
} 
+0

이러한 설명이 매우 좋다. 문제없이 이미지를 회전시킬 수 있지만 이미지를 회전 시키면 해상도가 떨어지는 유일한 세부 사항입니다. 그 이유는 무엇입니까? – Fabio

0

이 글에서 과거에 다음 코드를 사용한에게 절단 방지하는 것입니다. http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-rotate 다른 해결책도 있습니다.

/// <summary> 
/// Rotates the input image by theta degrees around center. 
/// </summary> 
public static Bitmap rotateCenter(Bitmap bmpSrc, float theta) 
{ 
    Matrix mRotate = new Matrix(); 
    mRotate.Translate(bmpSrc.Width/-2, bmpSrc.Height/-2, MatrixOrder.Append); 
    mRotate.RotateAt(theta, new Point(0, 0), MatrixOrder.Append); 
    using (GraphicsPath gp = new GraphicsPath()) 
    { // transform image points by rotation matrix 
     gp.AddPolygon(new Point[] { new Point(0, 0), new Point(bmpSrc.Width, 0), new Point(0, bmpSrc.Height) }); 
     gp.Transform(mRotate); 
     PointF[] pts = gp.PathPoints; 

     // create destination bitmap sized to contain rotated source image 
     Rectangle bbox = boundingBox(bmpSrc, mRotate); 
     Bitmap bmpDest = new Bitmap(bbox.Width, bbox.Height); 

     using (Graphics gDest = Graphics.FromImage(bmpDest)) 
     { // draw source into dest 
      Matrix mDest = new Matrix(); 
      mDest.Translate(bmpDest.Width/2, bmpDest.Height/2, MatrixOrder.Append); 
      gDest.Transform = mDest; 
      gDest.DrawImage(bmpSrc, pts); 
      //drawAxes(gDest, Color.Red, 0, 0, 1, 100, ""); 
      return bmpDest; 
     } 
    } 
} 

private static Rectangle boundingBox(Image img, Matrix matrix) 
{ 
    GraphicsUnit gu = new GraphicsUnit(); 
    Rectangle rImg = Rectangle.Round(img.GetBounds(ref gu)); 

    // Transform the four points of the image, to get the resized bounding box. 
    Point topLeft = new Point(rImg.Left, rImg.Top); 
    Point topRight = new Point(rImg.Right, rImg.Top); 
    Point bottomRight = new Point(rImg.Right, rImg.Bottom); 
    Point bottomLeft = new Point(rImg.Left, rImg.Bottom); 
    Point[] points = new Point[] { topLeft, topRight, bottomRight, bottomLeft }; 
    GraphicsPath gp = new GraphicsPath(points, 
                 new byte[] { (byte)PathPointType.Start, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line }); 
    gp.Transform(matrix); 
    return Rectangle.Round(gp.GetBounds()); 
} 
관련 문제