2014-01-31 61 views
2

需要創建一個函數,該函數在給定四個點的情況下裁剪圖像。通過指定四個角點(非矩形)來裁剪圖像

輸入:---------->圖像,四個角點

輸出:------->裁剪圖像

我所遇到兩種類型的裁剪功能到目前爲止,

1)使用的一個點,身高&寬度

2),其使用一個點,高度,寬度&的角度 然而這些功能似乎沒有完全解決我的問題。

爲了更清楚起見,我試圖在下圖中描述我的意圖。 Cropping an image, by specifying corner points

+0

請出示你試過代碼,並有... –

+0

公共靜態位圖CropRotatedRect問題(位圖源,矩形RECT,浮動角度,布爾高品質) { 位圖的結果=新位圖(rect.Width,RECT。高度);使用(Graphics g = Graphics.FromImage(result)) g.InterpolationMode = HighQuality? InterpolationMode.HighQualityBicubic:InterpolationMode.Default; (矩陣mat =新矩陣()) { mat.Translate(-rect.Location.X,-rect.Location.Y); mat.RotateAt(angle,rect.Location); g.Transform = mat; gDrawImage(source,new Point(0,0)); } } 返回結果; } – VAR

+0

我已經使用了上面的代碼,但是當輸入區域是梯形時,函數似乎不起作用。 如果有一個函數需要4點作爲輸入,然後處理它,這將是所有輸入區域類型的解決方案。至少,這就是我的感受 – VAR

回答

2

終於找到了解決我的問題:)

// define quadrilateral's corners 
List<IntPoint> corners = new List<IntPoint>(); 
corners.Add(new IntPoint( x1, y1)); 
corners.Add(new IntPoint(x2, y2)); 
corners.Add(new IntPoint(x3, y3)); 
corners.Add(new IntPoint(x4, y4)); 
// create filter 
QuadrilateralTransformation filter=new QuadrilateralTransformationBilinear(corners, NewWidth, NewHeight); 
// apply the filter 
Bitmap newImage = filter.Apply(image); 

這將任何四邊形轉變爲矩形之一。 它幫助了我,希望這些信息對其他人也有幫助。