2016-06-14 56 views
-4

自动裁剪我有这样的图像旋转并用C#

enter image description here

我如何裁剪和旋转用C#方? 我想自动做到这一点,我知道第一我应该边缘旋转和直接广场,但如何? 我想是这样的

enter image description here

+2

到目前为止您尝试了什么? – Rik

+1

对不起,但这太宽了:_Detect Largest Contour_ then _Image Transformation_;) –

+2

您应该先发布[MCVE] –

回答

1

这里有一个方法,你可以用它来旋转在C#中的图像:

/// <summary> 
/// method to rotate an image either clockwise or counter-clockwise 
/// </summary> 
/// <param name="img">the image to be rotated</param> 
/// <param name="rotationAngle">the angle (in degrees). 
/// NOTE: 
/// Positive values will rotate clockwise 
/// negative values will rotate counter-clockwise 
/// </param> 
/// <returns></returns> 
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 Point(0, 0)); 

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

    //return the image 
    return bmp; 
} 

您可以使用Graphics.DrawImage来绘制裁剪图像到图形对象来自位图。

Rectangle cropRect = new Rectangle(...); 
Bitmap src = Image.FromFile(fileName) as Bitmap; 
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height); 

using(Graphics g = Graphics.FromImage(target)) 
{ 
    g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height), 
        cropRect,       
        GraphicsUnit.Pixel); 
}