GetPixel Versus Dibsection -- A subtle problem
- From: cppaddict <hello@xxxxxxxxx>
- Date: Thu, 12 May 2005 10:54:14 GMT
I have two methods for getting pixel info from an application window.
One is to use GetPixel. The other is to create a bitmap using
CreateDibsection, select it into a memory DC, and BitBlt the window
pixels into the bitmap. Then we read the data directly from the
bitmap.
I've been having trouble getting my answers to agree in all cases, and
I think I'm making a very simple mistake in the code below. I need
help tracking it down. It might help to know that the answers agree
when I place the application window (whose pixels I'm trying to read)
in the upper left corner of the desktop. That is, the bitmap method
seems to be reading the upper left portion of the desktop, rather
than the upper left portion of the application window.
Code is below.... (the test fctn must be passed the HWND of the window
whose pixels you want to read)
Thanks very much in advance,
cpp
-----------------------
#include<windows.h>
#include<iostream>
// variables
static HWND capture = 0; // window we want to capture
static HBITMAP bitmap = 0; // dibsection
static BYTE* memory = 0; // dibsection memory
// functions
HBITMAP CreateDibsection24(HDC winDC, size_t dx, size_t dy, BYTE**
memory);
BOOL CaptureWindow();
void CompareMethods();
void test(HWND hwnd) {
//assume hwnd is the handle for an 800x600 application window
capture = hwnd
// create bitmap
HDC winDC = GetWindowDC(capture);
bitmap = CreateDibsection24(winDC, 800, 600, &memory);
ReleaseDC(capture,winDC);
//copy the application window pixels into the bitmap
CaptureWindow();
//see if this method agrees with GetPixel()
CompareMethods();
}
BOOL CaptureWindow()
{
// bitmap must be a dibsection
DIBSECTION ds;
if(!GetObject(bitmap, sizeof(ds), &ds)) { return FALSE; }
int bitmap_dx = ds.dsBmih.biWidth;
int bitmap_dy = ds.dsBmih.biHeight;
// get window dimensions
RECT rect;
GetWindowRect(capture, &rect);
int window_dx = rect.right - rect.left;
int window_dy = rect.bottom - rect.top;
// get window context
HDC winDC = GetWindowDC(capture);
if(!winDC) return FALSE;
// create a memory context to select the dibsection into
HDC memDC = CreateCompatibleDC(winDC);
// fill dibsection with black
// then copy the contents of the winDC into the dibsection
SelectObject(memDC, GetStockObject (BLACK_BRUSH));
SelectObject(memDC, bitmap);
PatBlt(memDC, 0, 0, bitmap_dx, bitmap_dy, PATCOPY);
BitBlt(memDC, 0, 0, window_dx, window_dy, winDC, 0, 0, SRCCOPY);
// cleanup contexts
DeleteDC(memDC);
ReleaseDC(capture, winDC);
return TRUE;
}
void CompareMethods()
{
// the pixel position to extract the color for
int x = 15;
int y = 15;
// use GetPixel
HDC winDC = GetWindowDC(capture);
COLORREF c1 = GetPixel(winDC, x, y);
ReleaseDC(capture, winDC);
// use dibsection memory, which is stored upside down
DIBSECTION ds;
GetObject(bitmap, sizeof(ds), &ds);
int pitch_bytes = (((24*ds.dsBmih.biWidth + 31) & (~31))/8);
y = (ds.dsBmih.biHeight - 1) - y;
int index_b = (pitch_bytes*y + 3*x);
int index_g = index_b + 1;
int index_r = index_b + 2;
COLORREF c2 = RGB(memory[index_r], memory[index_g], memory[index_b]);
// print colors to string
char buffer[256];
wsprintf(buffer, "cGetPixel = (%d,%d,%d) and cDibsection =
(%d,%d,%d)",
GetRValue(c1), GetGValue(c1), GetBValue(c1),
GetRValue(c2), GetGValue(c2), GetBValue(c2));
std::cout << buffer;
}
HBITMAP CreateDibsection24(HDC winDC, size_t dx, size_t dy, BYTE**
memory) {
// bitmap information
BITMAPINFOHEADER infoHeader;
infoHeader.biSize = sizeof(infoHeader);
infoHeader.biWidth = (LONG)dx;
infoHeader.biHeight = (LONG)dy;
infoHeader.biPlanes = 1;
infoHeader.biBitCount = 24;
infoHeader.biCompression = BI_RGB;
infoHeader.biSizeImage = 0;
infoHeader.biXPelsPerMeter = 0;
infoHeader.biYPelsPerMeter = 0;
infoHeader.biClrUsed = 0;
infoHeader.biClrImportant = 0;
// dibsection information
BITMAPINFO info;
info.bmiHeader = infoHeader;
return CreateDIBSection(winDC, &info, DIB_RGB_COLORS, (void**)memory,
0, 0);
}
.
- Follow-Ups:
- Re: GetPixel Versus Dibsection -- A subtle problem
- From: cppaddict
- Re: GetPixel Versus Dibsection -- A subtle problem
- From: Vipin
- Re: GetPixel Versus Dibsection -- A subtle problem
- Prev by Date: Re: how to create a new GDI+ image encoder ?
- Next by Date: Re: GetPixel Versus Dibsection -- A subtle problem
- Previous by thread: DrawFrameControl() and DFC_MENU question...
- Next by thread: Re: GetPixel Versus Dibsection -- A subtle problem
- Index(es):
Relevant Pages
|