Code
#include <windows.h>
#include <iostream>
using namespace std;
/////////////////////////////////////////////////////////////////////////////
//
// CompareBitmaps
// Compares two HBITMAPs to see if they contain the same image
//
// Parameters :
// HBitmapLeft [in] : The HBITMAP handles that are to be compared
// HBitmapRight [in] :
//
// Returns :
// true if the bitmaps are the same
// false if they are different
//
/////////////////////////////////////////////////////////////////////////////
bool CompareBitmaps(HBITMAP HBitmapLeft, HBITMAP HBitmapRight)
{
if (HBitmapLeft == HBitmapRight)
{
return true;
}
if (NULL == HBitmapLeft || NULL == HBitmapRight)
{
return false;
}
bool bSame = false;
HDC hdc = GetDC(NULL);
BITMAPINFO BitmapInfoLeft = {0};
BITMAPINFO BitmapInfoRight = {0};
BitmapInfoLeft.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfoRight.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
if (0 != GetDIBits(hdc, HBitmapLeft, 0, 0, NULL, &BitmapInfoLeft, DIB_RGB_COLORS) &&
0 != GetDIBits(hdc, HBitmapRight, 0, 0, NULL, &BitmapInfoRight, DIB_RGB_COLORS))
{
// Compare the BITMAPINFOHEADERs of the two bitmaps
if (0 == memcmp(&BitmapInfoLeft.bmiHeader, &BitmapInfoRight.bmiHeader,
sizeof(BITMAPINFOHEADER)))
{
// The BITMAPINFOHEADERs are the same so now compare the actual bitmap bits
BYTE *pLeftBits = (BYTE*)BitmapInfoLeft.bmiHeader.biSizeImage;
BYTE *pRightBits = (BYTE*)BitmapInfoRight.bmiHeader.biSizeImage;
BYTE *pByteLeft = NULL;
BYTE *pByteRight = NULL;
PBITMAPINFO pBitmapInfoLeft = &BitmapInfoLeft;
PBITMAPINFO pBitmapInfoRight = &BitmapInfoRight;
// calculate the size in BYTEs of the additional
// memory needed for the bmiColor table
int AdditionalMemory = 0;
switch (BitmapInfoLeft.bmiHeader.biBitCount)
{
case 1:
AdditionalMemory = 1 * sizeof(RGBQUAD);
break;
case 4:
AdditionalMemory = 15 * sizeof(RGBQUAD);
break;
case 8:
AdditionalMemory = 255 * sizeof(RGBQUAD);
break;
case 16:
case 32:
AdditionalMemory = 2 * sizeof(RGBQUAD);
}
if (AdditionalMemory)
{
// we have to allocate room for the bmiColor table that will be
// attached to our BITMAPINFO variables
pByteLeft = new BYTE[sizeof(BITMAPINFO) + AdditionalMemory];
if (pByteLeft)
{
memset(pByteLeft, 0, sizeof(BITMAPINFO) + AdditionalMemory);
memcpy(pByteLeft, pBitmapInfoLeft, sizeof(BITMAPINFO));
pBitmapInfoLeft = (PBITMAPINFO)pByteLeft;
}
pByteRight = new BYTE[sizeof(BITMAPINFO) + AdditionalMemory];
if (pByteRight)
{
memset(pByteRight, 0, sizeof(BITMAPINFO) + AdditionalMemory);
memcpy(pByteRight, pBitmapInfoRight, sizeof(BITMAPINFO));
pBitmapInfoRight = (PBITMAPINFO)pByteRight;
}
}
if (pLeftBits && pRightBits && pBitmapInfoLeft && pBitmapInfoRight)
{
// zero out the bitmap bit buffers
memset(pLeftBits, 0, BitmapInfoLeft.bmiHeader.biSizeImage);
memset(pRightBits, 0, BitmapInfoRight.bmiHeader.biSizeImage);
// fill the bit buffers with the actual bitmap bits
if (0 != GetDIBits(hdc, HBitmapLeft, 0,
pBitmapInfoLeft->bmiHeader.biHeight, pLeftBits, pBitmapInfoLeft,
DIB_RGB_COLORS) && 0 != GetDIBits(hdc, HBitmapRight, 0,
pBitmapInfoRight->bmiHeader.biHeight, pRightBits, pBitmapInfoRight,
DIB_RGB_COLORS))
{
// compare the actual bitmap bits of the two bitmaps
bSame = 0 == memcmp(pLeftBits, pRightBits,
pBitmapInfoLeft->bmiHeader.biSizeImage);
}
}
// clean up
delete[] pLeftBits;
delete[] pRightBits;
delete[] pByteLeft;
delete[] pByteRight;
}
}
ReleaseDC(NULL, hdc);
return bSame;
}
bool SaveBMPFile(char *filename, HBITMAP bitmap, HDC bitmapDC, int width, int height);
bool ScreenCapture(int x, int y, int width, int height, char *filename) {
// get a DC compat. w/ the screen
HDC hDc = CreateCompatibleDC(0);
// make a bmp in memory to store the capture in
HBITMAP hBmp = CreateCompatibleBitmap(GetDC(0), width, height);
// join em up
SelectObject(hDc, hBmp);
// copy from the screen to my bitmap
BitBlt(hDc, 0, 0, width, height, GetDC(0), x, y, SRCCOPY);
// save my bitmap
//bool ret = SaveBMPFile(filename, hBmp, hDc, width, height);
// free the bitmap memory
DeleteObject(hBmp);
return false;
}
void TestSS(int x, int y, int iWidth, int iHeight, int CopyX, int CopyY, HWND hwnd) {
HDC hDC, hDCMem, Source;
HBITMAP hBitmap;
// int iWidth, iHeight;
// Retrieve the handle to a display device context for the client
// area of the window.
hDC = GetDC(0);
Source = GetDC(hwnd);
// Create a memory device context compatible with the device.
hDCMem = CreateCompatibleDC(hDC);
// Retrieve the width and height of window display elements.
//iWidth = GetSystemMetrics(SM_CXSCREEN) / 10;
//iHeight = GetSystemMetrics(SM_CYSCREEN) / 10;
// Create a bitmap compatible with the device associated with the
// device context.
hBitmap = CreateCompatibleBitmap(hDC, iWidth, iHeight);
MessageBox(NULL, "BitBlt activating...", "WARNING", MB_ICONERROR);
//BitBlt(GetDC(0), 300, 300, 300, 300, GetDC(0), 0, 0, SRCCOPY);
BitBlt(Source, x, y, iWidth, iHeight, hDC, CopyX, CopyY, SRCCOPY);
// Delete the bitmap object and free all resources associated with it.
DeleteObject(hBitmap);
// Delete the memory device context and the display device context.
DeleteDC(hDCMem);
DeleteDC(hDC);
return;
}
void TestSS2(HWND hwnd, DWORD dwWindowX, DWORD dwWindowY) {
HDC hScreenDC = GetDC(hwnd); //global screen DC
HDC hCompatibleDC = CreateCompatibleDC(hScreenDC);
HBITMAP screenshot = CreateCompatibleBitmap(hScreenDC, dwWindowX, dwWindowY);
SelectObject(hCompatibleDC, screenshot);
BitBlt(hCompatibleDC, 50, 50, dwWindowX, dwWindowY, hScreenDC, 50, 50, SRCCOPY);
ReleaseDC(NULL, hScreenDC);
DeleteDC(hCompatibleDC);
return;
}
void Test3() {
HDC hdcScreen, hdcMemory;
int cxScreen, cyScreen;
HBITMAP hbmMemory, hbmOld;
BITMAPINFO bi;
void* data;
// get the HDC of the screen
hdcScreen = GetDC(NULL);
// get the screen size
cxScreen = GetSystemMetrics(SM_CXSCREEN) / 10;
cyScreen = GetSystemMetrics(SM_CYSCREEN) / 10;
// create a compatible memory dc/bitmap pair
hdcMemory = CreateCompatibleDC(hdcScreen);
hbmMemory = CreateCompatibleBitmap(hdcScreen, cxScreen, cyScreen);
hbmOld = (HBITMAP)SelectObject(hdcMemory, hbmMemory);
// "capture" the screen into this pair
BitBlt(hdcMemory, 0, 0, cxScreen, cyScreen, hdcScreen, 0, 0, SRCCOPY);
// now that we got the screen into the HBITMAP, get its bits
// we want to get it as 24-bpp
ZeroMemory(&bi, sizeof(BITMAPINFO));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = cxScreen;
bi.bmiHeader.biHeight = cyScreen;
bi.bmiHeader.biBitCount = 24;
bi.bmiHeader.biPlanes = 1;
// first call with a NULL pointer to fill in rest of bi.bmiHeader
GetDIBits(hdcMemory, hbmMemory, 0, cyScreen, NULL, &bi, DIB_RGB_COLORS);
// allocate memory (size is in bi.bmiHeader.biSizeImage)
data = malloc(bi.bmiHeader.biSizeImage);
// now get the data
GetDIBits(hdcMemory, hbmMemory, 0, cyScreen, data, &bi, DIB_RGB_COLORS);
if (CompareBitmaps(hbmMemory, hbmOld)) { MessageBox(0, "TRUE", "TRUE", MB_OK); } else { MessageBox(0, "FALSE", "FALSE", MB_OK); }
// clean-up
SelectObject(hdcMemory, hbmOld);
DeleteDC(hdcMemory);
DeleteObject(hbmMemory);
// thats it.
// data = the data
// 1. 24 bpp BGR format
// 2. 32-bit padded scanlines
// 3. upside down
// cxScreen = width
// cyScreen = height
return;
}
/*
int main() {
AllocConsole();
HANDLE outHandle = GetStdHandle(STD_OUTPUT_HANDLE);
//TestSS2(hwnd, 500, 500);
while (true) {
Sleep(50);
// if (GetAsyncKeyState(VK_HOME)) { Test3(); }
if (GetAsyncKeyState(VK_HOME)) {
HDC hDc = CreateCompatibleDC(0);
HBITMAP hBmp = CreateCompatibleBitmap(hDc, 50, 50);
BITMAP bmpOrig;
GetObject(hBmp, sizeof(BITMAP), &bmpOrig);
int sizeOrig = bmpOrig.bmWidthBytes * bmpOrig.bmHeight;
BYTE* pDataOrig = new BYTE[sizeOrig];
GetBitmapBits(hBmp, sizeOrig, pDataOrig);
SelectObject(hDc, hBmp);
char ImagePixel[500] = {'\0'};
sprintf(ImagePixel, "Image size: %d\n", sizeOrig);
WriteConsole(outHandle, ImagePixel, strlen(ImagePixel), NULL, NULL);
for (int x = 0; x < sizeOrig; x++) {
sprintf(ImagePixel, "pDataOrig[%d]: %d\n", x, pDataOrig[x]);
WriteConsole(outHandle, ImagePixel, strlen(ImagePixel), NULL, NULL);
}
SIZE Temp;
GetBitmapDimensionEx(hBmp, &Temp);
// long ImageSize = Temp.cx * Temp.cy;
// long *MyImage;
// GetBitmapBits(hBmp, ImageSize, MyImage);
//char ImagePixel[5000] = {'\0'};
// sprintf(ImagePixel, "Image size: %d\n", ImageSize);
// WriteConsole(outHandle, ImagePixel, strlen(ImagePixel), NULL, NULL);
// for (int x = 0; x < ImageSize; x++) {
// sprintf(ImagePixel, "%d\n", MyImage[x]);
// WriteConsole(outHandle, ImagePixel, strlen(ImagePixel), NULL, NULL);
// }
BitBlt(GetDC(0), 300, 300, 300, 300, GetDC(0), 0, 0, SRCCOPY);
DeleteObject(hBmp);
}
if (GetAsyncKeyState(VK_END)) { exit(0); }
}
//TestSS(250, 250, 500, 500, 50, 50, hwnd);
// HDC hDc = CreateCompatibleDC(0);
// HBITMAP hBmp = CreateCompatibleBitmap(GetDC(0), 50, 50);
// SelectObject(hDc, hBmp);
// BitBlt(GetDC(0), 300, 300, 300, 300, GetDC(0), 0, 0, SRCCOPY);
// DeleteObject(hBmp);
return 0;
}
*/
int main() {
AllocConsole();
HANDLE outHandle = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hBitMap = LoadImage(0, "C:\\Documents and Settings\\Compaq_Owner\\My Documents\\My Shit\\Programming projects\\Current\\Runescape bots\\Runescape Library\\Screen shot\\img.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
char ImagePixel[500] = {'\0'};
BITMAP bitmap;
GetObject(hBitMap,sizeof(BITMAP),&bitmap);
int size = bitmap.bmHeight*bitmap.bmWidth*bitmap.bmBitsPixel/8;
BYTE *lpBits = new BYTE[ size ];
GetBitmapBits((HBITMAP)hBitMap,size,lpBits );
HANDLE hBitMap2 = LoadImage(0, "C:\\Documents and Settings\\Compaq_Owner\\My Documents\\My Shit\\Programming projects\\Current\\Runescape bots\\Runescape Library\\Screen shot\\img2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
BITMAP bitmap2;
GetObject(hBitMap2,sizeof(BITMAP),&bitmap2);
int size2 = bitmap2.bmHeight*bitmap2.bmWidth*bitmap2.bmBitsPixel/8;
BYTE *lpBits2 = new BYTE[ size2 ];
GetBitmapBits((HBITMAP)hBitMap2,size2,lpBits2 );
cout << bitmap2.bmBitsPixel;
sprintf(ImagePixel, "bitmap2.bmBitsPixel %d", bitmap2.bmBitsPixel);
WriteConsole(outHandle, ImagePixel, strlen(ImagePixel), NULL, NULL);
int ScreenWidth = bitmap2.bmWidth;
int ScreenHeight = bitmap2.bmHeight;
int numOfCard = 1;
int cardHeight = bitmap.bmHeight;
int card_line_length = bitmap.bmWidth;
for( int i = 0; i < ScreenWidth; i++ ) {
for( int j = 0; j < ScreenHeight; j++ ) {
for ( int k= 0; k < numOfCard; k++ ) {
int tmpY = j;
for ( int x = 0; x < cardHeight; x++ ) {
sprintf(ImagePixel, "%d", tmpY);
WriteConsole(outHandle, ImagePixel, strlen(ImagePixel), NULL, NULL);
}
}
}
}
system("PAUSE");
return 0;
}
A collection of various functions I found googling a while back, I haven't gone back to trying to get this to work, but I'd like to start on it again.
My question is: How do you compare one image against another?