I noticed a weird behavior with the Graphics.DrawImage
function when used to crop bitmap.
Those two sample
RectangleF area = new RectangleF();
area.X = X / ratio.X;
area.Y = Y / ratio.Y;
area.Width = W / ratio.X;
area.Height = H / ratio.Y;
// Clone way
var croppedBitmap = imageToProcess.Clone(area, imageToProcess.PixelFormat);
And
RectangleF area = new RectangleF();
area.X = X / ratio.X;
area.Y = Y / ratio.Y;
area.Width = W / ratio.X;
area.Height = H / ratio.Y;
// Graphics way
var croppedBitmapg = new Bitmap((int) area.Width, (int) area.Height);
using (Graphics g = Graphics.FromImage(croppedBitmapg))
{
g.DrawImage(imageToProcess, new Rectangle(0, 0, croppedBitmapg.Width, croppedBitmapg.Height), area, GraphicsUnit.Pixel);
};
Are not producing the exact same bitmap image output.
The thing is, Graphics way is faster than Clone (0.05x vs 0.00x in s)
BUT
using Graphics output for OCR (with Tesseract) does not give results. However, same bitmap produced by Clone method gives perfect result. I'd like to understand why, since images properties and visual seems to be the same.
Outputs and properties (Clone | Graphics)