Quick comparison of two Bitmap objects per pixel per pixel

I am currently implementing a method that accepts two raster objects. We can assume that these objects have the same size, etc. The method return is a list of pixel changes (this is stored in a makeshift object). This is being developed in iterative order, so the current implementation was basic ... just work through each pixel and compare it with its copy. This way of generating changes is slower than acceptable (500 ms or so), so I'm looking for a faster process.

The ideas that crossed my mind are to split the image into stripes and start each comparison in a new stream or compare screen areas as objects at first, and then only examine them in detail as necessary.

current code for your understanding ...

for (int x = 0; x < screenShotBMP.Width; x++) { for (int y = 0; y < screenShotBMP.Height; y++) { if (screenShotBMP.GetPixel(x, y) != _PreviousFrame.GetPixel(x, y)) { _pixelChanges.Add(new PixelChangeJob(screenShotBMP.GetPixel(x,y), x, y)); } } } 

As you subtract from the code, the concept of the class in question is to take a screenshot and generate a list of pixel changes from a previously taken screenshot.

+4
source share
2 answers

You should definitely look at the Lockbits raster data management method.

This is an order of magnitude faster than GetPixel / SetPixel.

EDIT:
Check this link for some code (although in VB, but you should get a drift), which almost does what you want. It just checks two bitmaps for equality and returns true or false. You can change the function so that each pixel check adds _pixelChanges to your list, if necessary, and returns this list instead of a boolean.

Also, it can be faster if you exchange iterator loops. that is, the inner loop iterating over X, and the outer loop iterating over Y.

+3
source

Use bitBlt with the XOR option ..... It should be much faster.

+3
source

All Articles