0

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)

Comparaison

  • *"The thing is, Graphics way is faster than Clone"* - it's not clear what time you measure and how exactly. Would you mind to post the test code? – Sinatr May 15 '18 at 10:32
  • Both of the blocks are exactly the function in my code. I measure them by stocking `DateTime.UtcNow.Miliseconds` before the rectangle initialization and substract new `DateTime.UtcNow.Miliseconds` by old one just after each block (but still in the same function). I would share a test code but, the only difference between those two produced bitmaps is the tesseract OCR output.. – Sebastien Servouze May 15 '18 at 11:53
  • 2
    You are measuring it [wrong](https://stackoverflow.com/a/14019526/1997232). – Sinatr May 15 '18 at 13:16
  • Oh, yeah actually ! I'd still like to understand why the ouputs differs – Sebastien Servouze May 15 '18 at 13:25
  • How are you determining that they are "not producing the exact same bitmap image output"? I cut out both pieces from your image and saved them in gimp and the png files were identical. – Nyerguds May 15 '18 at 14:20
  • Because Tesseract use images to recognize characters from them, and both images doesn't give the same output, cloned one is recognized perfectly and the other is not recognized at all – Sebastien Servouze May 15 '18 at 14:31
  • First thing to do is to [test properly](https://stackoverflow.com/a/1622491/1997232). `Clone` method may do more than just bit-blitting. This is speculation because it works with `float`. Maybe it has more parameters and you just using the overload with many parameters in defaults, but what are defaults? What documentation say? – Sinatr May 15 '18 at 14:32
  • I think whatever's going wrong with your non-cloned drawn image is probably not the method of creation... there may be something in code you haven't posted here that makes it not recognised by Tesseract. – Nyerguds May 15 '18 at 16:00

0 Answers0