Reputation: 2505
I am trying to get an image for every corner of a scanned document ( top left and right, bottom left and right). Below is how i attempted to implement that but when i looked at the saved images they are all different parts but of the top left corner only and not the whole document. Any suggestions to how i can change this?
Bitmap result = fullImg;
//top-left
var bandImg1 = result.Clone(new System.Drawing.Rectangle(0, 0, 375, 375), fullImg.PixelFormat);
//top-right
var bandImg2 = result.Clone(new System.Drawing.Rectangle(100, 50, 375, 375), fullImg.PixelFormat);
//bottom-left
var bandImg3 = result.Clone(new System.Drawing.Rectangle(0, 50, 375, 375), fullImg.PixelFormat);
//bottom-right
var bandImg4 = result.Clone(new System.Drawing.Rectangle(100, 100, 375, 375), fullImg.PixelFormat);
bandImg1.Save("c:\\bandImg1.gif", System.Drawing.Imaging.ImageFormat.Gif);
bandImg2.Save("c:\\bandImg2.gif", System.Drawing.Imaging.ImageFormat.Gif);
bandImg3.Save("c:\\bandImg3.gif", System.Drawing.Imaging.ImageFormat.Gif);
bandImg4.Save("c:\\bandImg4.gif", System.Drawing.Imaging.ImageFormat.Gif);
----- Updated code with additions based on answer below ------
Bitmap result = fullImg;
//top-left
var bandImg1 = result.Clone(new System.Drawing.Rectangle(0, 0, result.Width/2, result.Height/2), fullImg.PixelFormat);
//top-right
var bandImg2 = result.Clone(new System.Drawing.Rectangle(result.Width / 2, 0, result.Width / 2, result.Height / 2), fullImg.PixelFormat);
//bottom-left
var bandImg3 = result.Clone(new System.Drawing.Rectangle(0, result.Height / 2, result.Width / 2, result.Height / 2), fullImg.PixelFormat);
//bottom-right
var bandImg4 = result.Clone(new System.Drawing.Rectangle(result.Width / 2, result.Height / 2, result.Width / 2, result.Height / 2), fullImg.PixelFormat);
bandImg1.Save("c:\\bandImg1.gif", System.Drawing.Imaging.ImageFormat.Gif);
bandImg2.Save("c:\\bandImg2.gif", System.Drawing.Imaging.ImageFormat.Gif);
bandImg3.Save("c:\\bandImg3.gif", System.Drawing.Imaging.ImageFormat.Gif);
bandImg4.Save("c:\\bandImg4.gif", System.Drawing.Imaging.ImageFormat.Gif);
string QRinfo = Process(bandImg1);
Process Method:
public string Process(Bitmap bitmap)
{
var reader = new com.google.zxing.qrcode.QRCodeReader();
try
{
LuminanceSource source = new RGBLuminanceSource(bitmap, bitmap.Width, bitmap.Height);
var binarizer = new HybridBinarizer(source);
var binBitmap = new BinaryBitmap(binarizer);
return reader.decode(binBitmap).Text;
}
catch (Exception e)
{
return e.Message;
}
}
Upvotes: 1
Views: 832
Reputation: 647
My guess is that you need to take the width and height of result
, or fullImg
into account when calculating the rectangle coordinates, instead of hardcoding values like you are. Something like this:
//top left
var bandImg1 = result.Clone(new Rectangle(0, 0, result.Width / 2, result.Height / 2), result.PixelFormat);
//top right
var bandImg2 = result.Clone(new Rectangle(result.Width / 2, 0, result.Width / 2, result.Height / 2), result.PixelFormat);
//bottom left
var bandImg3 = result.Clone(new Rectangle(0, result.Height / 2, result.Width / 2, result.Height / 2), result.PixelFormat);
//bottom right
var bandImg4 = result.Clone(new Rectangle(result.Width / 2, result.Height / 2, result.Width / 2, result.Height / 2), result.PixelFormat);
What you're doing now is probably not working because you are hardcoding the values. For example, for bandImg1
you are using (0,0,375,375)
for the rectangle which is basically saying "start at the top left, and go 375 pixels to the right, and 375 pixels down". Instead, you want to use (0, 0, result.Width / 2, result.Height / 2)
which says "start at the top left, go halfway across the image, then go halfway down the image", which will give you the upper left corner.
Upvotes: 1