Click to See Complete Forum and Search --> : window into byte array


dave2k
January 25th, 2006, 07:47 AM
hi. the following code i have creates a BYTE array from the caculator screen. I would apperciate any helpful comments on this code on what could be improved, whether this is the right/shortest wayto do this, etc..


thanks!int _tmain(int argc, _TCHAR* argv[])
{
RECT rc;
HWND hWnd = ::FindWindow(0, _T("Calculator"));
if(!hWnd) return 0;

::GetWindowRect(hWnd, &rc);
int w = rc.right-rc.left;
int h = rc.bottom-rc.top;

HDC hdc = ::GetDC(0);
HDC memDC = ::CreateCompatibleDC ( hdc );
HBITMAP memBM = ::CreateCompatibleBitmap ( hdc, w, h );
::SelectObject ( memDC, memBM );
BITMAP bitmap2;
::GetObject(memBM, sizeof(BITMAP), &bitmap2);
int BigBps = bitmap2.bmBitsPixel / 8;
int bigSize = bitmap2.bmHeight * bitmap2.bmWidth * BigBps;
BYTE *bigBytes = new BYTE[bigSize];
::GetBitmapBits(memBM, bigSize, bigBytes );

_getch();
return 0;
}

dave2k
January 25th, 2006, 07:57 AM
i tried to use the code above to create a bmp file, but it just comes out blank. any idea why?

dave2k
January 25th, 2006, 08:33 AM
ok i used RECT rc;
HWND hWnd = ::FindWindow(0, _T("Calculator"));
::GetWindowRect (hWnd, &rc);
HDC hDC = ::GetDC(0);
HDC memDC = ::CreateCompatibleDC(hDC);
HBITMAP memBM = ::CreateCompatibleBitmap(hDC, rc.right-rc.left, rc.bottom-rc.top);
HBITMAP OldBM = (HBITMAP)::SelectObject(memDC, memBM);
::BitBlt(memDC, 0, 0, rc.right-rc.left, rc.bottom-rc.top, hDC, rc.left, rc.top, SRCCOPY);
int Bps = ::GetDeviceCaps(hDC, BITSPIXEL);
int Width = rc.right-rc.left;
int Height = rc.bottom-rc.top;
int size = Bps/8 * (Width * Height);
BYTE *lpBits = new BYTE[size];
::GetBitmapBits(memBM, size, lpBits);

kirants
January 25th, 2006, 09:16 AM
Is this what you are looking for ?
Capturing Windows Regardless of Their Z-Order (http://www.codeguru.com/cpp/g-m/gdi/capturingimages/article.php/c11231/)

dave2k
January 25th, 2006, 11:48 AM
hi kirants, i don't know if you can help me but what i am trying to do is load a smaller bitmap, and see whether it appears in an image of the calculator screen. my code is below. i have also attached a picture of an image which works and one which doesn't. It's really weird some some are found and other's aren't when they all exist within the larger image. i have been trying to get this to work properly for months now :(void main()
{
// small image
HANDLE hBitMap1 = ::LoadImage(0, _T("c:\\ps2.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
BITMAP bitmap1;
::GetObject(hBitMap1, sizeof(BITMAP), &bitmap1);
int SmallBps = bitmap1.bmBitsPixel / 8;
int smallSize = bitmap1.bmHeight * bitmap1.bmWidth * SmallBps;
BYTE *smallBytes = new BYTE[smallSize];
::GetBitmapBits((HBITMAP)hBitMap1, smallSize, smallBytes);

RECT rc;
HWND hWnd = ::FindWindow(0, _T("Calculator"));
::GetWindowRect (hWnd, &rc);
HDC hDC = ::GetDC(0);
HDC memDC = ::CreateCompatibleDC(hDC);
HBITMAP memBM = ::CreateCompatibleBitmap(hDC, rc.right-rc.left, rc.bottom-rc.top);
HBITMAP OldBM = (HBITMAP)::SelectObject(memDC, memBM);
::BitBlt(memDC, 0, 0, rc.right-rc.left, rc.bottom-rc.top, hDC, rc.left, rc.top, SRCCOPY);
int BigBps = ::GetDeviceCaps(hDC, BITSPIXEL)/8;
int Width = rc.right-rc.left;
int Height = rc.bottom-rc.top;
int size = BigBps * (Width * Height);
BYTE *bigBytes = new BYTE[size];
::GetBitmapBits(memBM, size, bigBytes);

int NumOfFounds = 0;
int j;
for(int i = 0; i < Height; i ++)
{
for(int hh = 0; hh < Width; hh ++)
{
for(j = 0; j < bitmap1.bmHeight; j ++)
{
int PosOnSmall = j * bitmap1.bmWidth * SmallBps;
int PosOnBig = (i*Width * BigBps) + (hh*BigBps)+ j * Width * BigBps;
int SmallLineWidth = bitmap1.bmWidth * SmallBps;
if(memcmp(&bigBytes[PosOnBig], &smallBytes[PosOnSmall], SmallLineWidth) != 0)
break;
}
if ( j == bitmap1.bmHeight )
{
// Found
NumOfFounds++;
}
}
}

printf("%d", NumOfFounds);If working it should print out the number of smaller images inside the larger one. Obviously the calculator needs to be open/visible.

It seems like if the smaller image has buttons/radio buttons/check boxes never work...

dave2k
January 26th, 2006, 04:38 AM
would i be right in thinking this isn't working because the file i am loading is a DIB while the calculator image is a DDB?