2012-11-04 36 views
-2

我想從整數x,y座標在C#中裁剪一張圖片.. 我只是無法弄清楚如何得到它?裁剪圖片,從X,Y獲取矩形?

public void doCroppedImage(int pointX, int pointY) 
{ 
    Rectangle cropRect = //??? 
} 
+7

這似乎是因爲你還沒有寫任何代碼...... –

+2

什麼是'pointX','pointY'? x在loword和y在hiword中的座標?或者只是同一點的x和y,(0,0)總是另一個點? – GSerg

+5

一點不做矩形。基本幾何。請參閱[矩形結構](http://msdn.microsoft.com/en-us/library/system.drawing.rectangle.aspx)。 –

回答

10

您可以使用此代碼。它返回裁剪後的圖像。

public static Bitmap CropImage(Image source, int x,int y,int width,int height) 
{ 
    Rectangle crop = new Rectangle(x, y, width, height); 

    var bmp = new Bitmap(crop.Width, crop.Height); 
    using (var gr = Graphics.FromImage(bmp)) 
    { 
     gr.DrawImage(source, new Rectangle(0, 0, bmp.Width, bmp.Height), crop, GraphicsUnit.Pixel); 
    } 
    return bmp; 
} 

但一個評論,裁剪你要知道,不僅x和剪切點的y座標,而且裁剪圖像的寬度和高度的圖像。