2016-06-14 5 views
-4

Ich habe dieses BildDrehen und Zuschneiden automatisch mit C#

enter image description here

Wie kann ich mit C# zuschneiden und drehen Quadrat? Ich möchte dies automatisch tun, ich weiß zuerst, ich sollte Kanten zu drehen und direktes Quadrat, Aber wie? Ich möchte

so etwas wie dieses Sie verwenden können, hier ist eine Methode

enter image description here

+2

Was haben Sie bisher versucht? – Rik

+1

Sorry Kumpel, aber das ist zu breit: _Detect Largest Contour_ dann _Image Transformation_;) –

+2

Sie sollten mit der Veröffentlichung eines [MCVE] beginnen –

Antwort

1

bekommen ein Bild in C# drehen:

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

Sie können Graphics.DrawImage verwenden eine beschnittene Bild auf die zeichnen Grafikobjekt aus einer Bitmap.

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