裁剪位图会得到空位图
我从这里(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
I've taken a code from here (SO) that crops images. I tried it on bitmaps containing black text on white font. The result i get in return is a completely white output with no content.
// 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;
Any guess?
PS if i crop in paint of course it does work
edit
If I crop a picture of me for example it works....
Appendix
Code:
Rectangles:
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;
}
Image:
(Sensitive data is hidden, I only left the part I'm trying to crop)
http://img33.imageshack.us/img33/5703/modelx.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
固定代码:
Fixed code: