我從這裏(SO)拍攝了一個代碼來裁剪圖像。我在包含白色字體上的黑色文本的位圖上試過。我得到的結果是一個完全沒有內容的白色輸出。裁剪一個位圖給出一個空位圖
// create new bitmap with desired size and same pixel format
Bitmap croppedBitmap = new Bitmap(rect.Width, rect.Height, bitmap.PixelFormat);
// create Graphics "wrapper" to draw into our new bitmap
// "using" guarantees a call to gfx.Dispose()
using (Graphics gfx = Graphics.FromImage(croppedBitmap))
{
// draw the wanted part of the original bitmap into the new bitmap
gfx.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
}
return croppedBitmap;
任何猜測?
PS如果我當然油漆裁剪它的工作
編輯
如果我裁剪我的照片,例如工程....
附錄
代碼:
矩形:
Rectangle 1: 8 50, 95, 80, 30 // invoice number
Rectangle 2: 625, 778, 475, 22 // Total amount
CropImage():
public static Bitmap CropImage(Bitmap bitmap, Rectangle rect)
{
Bitmap croppedBitmap = new Bitmap(rect.Width, rect.Height, bitmap.PixelFormat);
using (Graphics gfx = Graphics.FromImage(croppedBitmap))
{
gfx.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
}
return croppedBitmap;
}
圖片: (敏感數據被隱藏,我只剩下我試圖裁剪部分) http://img33.imageshack.us/img33/5703/modelx.png
您是否調試過程序並檢查代碼中的所有變量是否保存了正確的數據?這可能看起來像一個錯誤的路徑問題或類似的問題恕我直言 – BigFatBaby 2011-03-31 10:38:53
是的,我做了我的變量是好的 – CoolStraw 2011-03-31 10:39:11
問題從你已經採取代碼的鏈接。 – 2011-03-31 10:41:23