Accessing Pixels with CreateDIBSection
-
Hello all. I am new to image processing and am having trouble with CreateDIBSection. I have been reading other threads on this topic and code snippets for several days now, so I was hoping at this point to get specific help with my code rather than a link to a previous explanation or code. I have a dialog based app. I want to take a screen shot, change some of the pixel colors and BitBlt it back. In my attempt to make a rectangle of red pixels (or any color) that is 800 pixels in width and 900 in height, I end up with 4 rectangles that are 200 pixels in width and 225 pixels in height, that are separated by 36 pixels and that are alternating is color between red a blue. I'm guessing that this has something to do with the padding. I'd really appreciate an help you can offer!
CDC\* pDC=GetDC(); HDC hDC = \*pDC; HDC hDCMem = CreateCompatibleDC(hDC); BYTE\* lpBitmapBits = NULL; BITMAPINFO bi; ZeroMemory(&bi, sizeof(BITMAPINFO)); bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bi.bmiHeader.biWidth = m\_rcMainRect.Width(); bi.bmiHeader.biHeight = -m\_rcMainRect.Height(); bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 32; HBITMAP bitmap = ::CreateDIBSection(hDCMem, &bi, DIB\_RGB\_COLORS, (LPVOID\*)&lpBitmapBits, NULL, 0); HGDIOBJ oldbmp = ::SelectObject(hDCMem, bitmap); BitBlt(hDCMem, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDC, 0, 0, SRCCOPY); for(int x=0; x<800; x=x+4) { for(int y=0; y<900; y++) { lpBitmapBits\[(y\*m\_rcMainRect.Width())+x\] = 0x00; //alpha channel? lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+1\] = 0x00; lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+2\] = 0xFF; lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+3\] = 0x00; } } BitBlt(hDC, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDCMem, 0, 0, SRCCOPY); SelectObject(hDCMem,oldbmp); DeleteDC(hDCMem); DeleteObject(bitmap);
-
Hello all. I am new to image processing and am having trouble with CreateDIBSection. I have been reading other threads on this topic and code snippets for several days now, so I was hoping at this point to get specific help with my code rather than a link to a previous explanation or code. I have a dialog based app. I want to take a screen shot, change some of the pixel colors and BitBlt it back. In my attempt to make a rectangle of red pixels (or any color) that is 800 pixels in width and 900 in height, I end up with 4 rectangles that are 200 pixels in width and 225 pixels in height, that are separated by 36 pixels and that are alternating is color between red a blue. I'm guessing that this has something to do with the padding. I'd really appreciate an help you can offer!
CDC\* pDC=GetDC(); HDC hDC = \*pDC; HDC hDCMem = CreateCompatibleDC(hDC); BYTE\* lpBitmapBits = NULL; BITMAPINFO bi; ZeroMemory(&bi, sizeof(BITMAPINFO)); bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bi.bmiHeader.biWidth = m\_rcMainRect.Width(); bi.bmiHeader.biHeight = -m\_rcMainRect.Height(); bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 32; HBITMAP bitmap = ::CreateDIBSection(hDCMem, &bi, DIB\_RGB\_COLORS, (LPVOID\*)&lpBitmapBits, NULL, 0); HGDIOBJ oldbmp = ::SelectObject(hDCMem, bitmap); BitBlt(hDCMem, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDC, 0, 0, SRCCOPY); for(int x=0; x<800; x=x+4) { for(int y=0; y<900; y++) { lpBitmapBits\[(y\*m\_rcMainRect.Width())+x\] = 0x00; //alpha channel? lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+1\] = 0x00; lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+2\] = 0xFF; lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+3\] = 0x00; } } BitBlt(hDC, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDCMem, 0, 0, SRCCOPY); SelectObject(hDCMem,oldbmp); DeleteDC(hDCMem); DeleteObject(bitmap);
The width of the bitmap is in pixels (DWORDs) and your buffer is in BYTEs ... :) PS: You can use all GDI functions in the memory-DC as well.
They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)
-
The width of the bitmap is in pixels (DWORDs) and your buffer is in BYTEs ... :) PS: You can use all GDI functions in the memory-DC as well.
They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)
Thanks so much for the quick reply. You are a lifesaver! (Or... at least a sleep saver.) :)
-
Hello all. I am new to image processing and am having trouble with CreateDIBSection. I have been reading other threads on this topic and code snippets for several days now, so I was hoping at this point to get specific help with my code rather than a link to a previous explanation or code. I have a dialog based app. I want to take a screen shot, change some of the pixel colors and BitBlt it back. In my attempt to make a rectangle of red pixels (or any color) that is 800 pixels in width and 900 in height, I end up with 4 rectangles that are 200 pixels in width and 225 pixels in height, that are separated by 36 pixels and that are alternating is color between red a blue. I'm guessing that this has something to do with the padding. I'd really appreciate an help you can offer!
CDC\* pDC=GetDC(); HDC hDC = \*pDC; HDC hDCMem = CreateCompatibleDC(hDC); BYTE\* lpBitmapBits = NULL; BITMAPINFO bi; ZeroMemory(&bi, sizeof(BITMAPINFO)); bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bi.bmiHeader.biWidth = m\_rcMainRect.Width(); bi.bmiHeader.biHeight = -m\_rcMainRect.Height(); bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 32; HBITMAP bitmap = ::CreateDIBSection(hDCMem, &bi, DIB\_RGB\_COLORS, (LPVOID\*)&lpBitmapBits, NULL, 0); HGDIOBJ oldbmp = ::SelectObject(hDCMem, bitmap); BitBlt(hDCMem, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDC, 0, 0, SRCCOPY); for(int x=0; x<800; x=x+4) { for(int y=0; y<900; y++) { lpBitmapBits\[(y\*m\_rcMainRect.Width())+x\] = 0x00; //alpha channel? lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+1\] = 0x00; lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+2\] = 0xFF; lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+3\] = 0x00; } } BitBlt(hDC, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDCMem, 0, 0, SRCCOPY); SelectObject(hDCMem,oldbmp); DeleteDC(hDCMem); DeleteObject(bitmap);
A good advice for the future: The size in bytes occupied by one row of pixels in memory is called stride. Its important that in bitmaps stride is always a multiple of 4 bytes. In your cases this condition is true in all cases regardless of the bitmap width because 1 pixel occupies 4 bytes. But for example in a 8 bit bitmap (256 colors) with width==3 the stride would be 4 bytes regardless of the actual size being 3 bytes, there would go 1 byte extra padding. stride = ((width * bits_per_pixel + 31) >> 3) & ~3;
-
A good advice for the future: The size in bytes occupied by one row of pixels in memory is called stride. Its important that in bitmaps stride is always a multiple of 4 bytes. In your cases this condition is true in all cases regardless of the bitmap width because 1 pixel occupies 4 bytes. But for example in a 8 bit bitmap (256 colors) with width==3 the stride would be 4 bytes regardless of the actual size being 3 bytes, there would go 1 byte extra padding. stride = ((width * bits_per_pixel + 31) >> 3) & ~3;
Thank you!
-
Thank you!
You are welcome!