James Newton
James Newton

Reputation: 777

Create Memory Device Context... for a bitmap, not a device (not display)

I'm probably thinking about this all wrong, so please let me start by stating the goal: I want to compare two images by XORing them together, then save (not display) the result as a file. Actually, I want to then average all the bit values to get a percentage match, but that part isn't hard.

So BitBlt (remember BitBlt?) has a ROP called SRCINVERT that does exactly this. And screamingly fast. Works a treat. But... it wants a source and destination DC (Device Context) to work with... you can SelectObject a bitmap into those DC's, but you must have the DC's.

Making DC's is easy with CreateCompatibleDC(0); or even CreateDC(L"DISPLAY", NULL, NULL, NULL); but... those are based on the pixel depth, size, etc... of the current hardware display. {EDIT: Nop, CreateCompatibleDC(0); gets everything from the bitmap selected into it, I just wasn't actually using it.} And I don't care about that, I want to work with the pixel depth, etc... of the files I'm loading the bitmaps from. In fact, this is a command line program; it will never have a window. EDIT: But because the DC is for the actual screen, when using CreateDC(L"DISPLAY", NULL, NULL, NULL), I see the image on the display (overwriting the desktop) before it is saved out to a file.

Am I stuck? Do I have to find another graphics library to do my XOR function? Or is there a way to use BitBlt? More generally, is there a way to do GDI or GDIPlus type stuff with DC's that don't exist on the current system? Wouldn't that be a really useful thing to be able to do for programs that process image files without necessarily displaying the result?

Upvotes: 1

Views: 942

Answers (2)

James Newton
James Newton

Reputation: 777

nevermind... silly boy... I was using CreateDC(L"DISPLAY", NULL, NULL, NULL); to make the destination DC and when I tried CreateCompatibleDC(0); I must have only changed the source DC... Oops. CreateCompatibleDC(0); works a treat.

https://github.com/JamesNewton/WinBitBltImageCompare

Upvotes: 0

Seva Alekseyev
Seva Alekseyev

Reputation: 61398

Once you select a bitmap into a memory context, its bit depth/pixel size will be taken over by the ones from the bitmap.

Upvotes: 2

Related Questions