Edit pixels in a bitmap?

I want to load a 24 bit bitmap, search for specific colors and change them to other colors. GetPixel and SetPixel is extremely slow but I came up with something else that seems to work, here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
BITMAP bitmap;
GetObject(hBmp, sizeof(BITMAP), &bitmap);

DWORD bmpLen = bitmap.bmWidthBytes * bitmap.bmHeight;
BYTE* bmp = new BYTE[bmpLen];
COLORREF cr = 0;

GetBitmapBits(hBmp, bmpLen, bmp);

for (DWORD i = 0; i < bmpLen; i += (bitmap.bmBitsPixel / 8))
{
       CopyMemory(&cr, &bmp[i], sizeof(COLORREF));
       // ----> CHANGE COLOR
       CopyMemory(&bmp[i], &cr, sizeof(COLORREF));
}


It seems to work but when I save the bmp array to a binary file the file becomes larger than the original bitmap by about 200 kb which must mean somehow I didn't calculate the bitmap size correctly. What am I doing wrong?
Last edited on
The arguments of CopyMemory() are incorrect. See http://msdn.microsoft.com/en-us/library/windows/desktop/aa366535(v=vs.85).aspx .
Oops. I typed the code out instead of copying & pasting and got CopyMemory confused for GetBitmapBits. I edited my post to fix it.
Unsure where the problem is. I mean, there's a potential for corruption with your code on less-than-sizeof(COLORREF) bytes per pixel, but corruption shouldn't increase size unless Windows recalculates a bitmap based on the new bits.

So show your full conversion code, including how you set the bits after color modification.
Topic archived. No new replies allowed.