I have got a trouble using the StretchDIBits function.
I want to draw a bitmap made from a buffer. However, the colors I define in the buffer are different from the result on screen.
I have read the documentation and I played with the biCompression (BI_RGB and BI_BITFIELDS) and biClrUsed (0 / 3) parameters of the BITMAPINFOHEADER. I can see some differences depending on their values, but the result is still different from what I am expecting.
Here is the code I am using (it can be inserted in the OnDraw method of a template SDI project to demonstrate the problem).
void CTestStretchDIBitsView::OnDraw(CDC* /*pDC*/)
{
...
CClientDC dc(this);
CRect rect;
GetClientRect(&rect);
DWORD* pBuffer = new DWORD[500 * 500];
memset(pBuffer, RGB(255, 255, 0), 500 * 500 * sizeof(DWORD));
LPBITMAPINFO pBmpInfo = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pBmpInfo->bmiHeader.biWidth = 500;
pBmpInfo->bmiHeader.biHeight = 500;
pBmpInfo->bmiHeader.biPlanes = 1;
pBmpInfo->bmiHeader.biBitCount = 32;
pBmpInfo->bmiHeader.biCompression = BI_BITFIELDS;
pBmpInfo->bmiHeader.biSizeImage = 500 * 500;
pBmpInfo->bmiHeader.biXPelsPerMeter = 0;
pBmpInfo->bmiHeader.biYPelsPerMeter = 0;
pBmpInfo->bmiHeader.biClrUsed = 0;
pBmpInfo->bmiHeader.biClrImportant = 0;
SetStretchBltMode(dc.m_hDC, STRETCH_DELETESCANS);
StretchDIBits(dc.m_hDC,
0,
rect.Height(),
rect.Width(),
-rect.Height(),
0,
0,
500,
500,
pBuffer,
pBmpInfo,
DIB_RGB_COLORS,
SRCCOPY);
delete[] pBmpInfo;
delete[] pBuffer;
}
You have to use the following mode
SetStretchBltMode(hdcWindow,HALFTONE);
instead of
SetStretchBltMode(dc.m_hDC, STRETCH_DELETESCANS);
because halftone is the best mode according to my research.
The problem didn't come from the StretchDIBits function but from the initialization of the buffer used as the bitmap here.
memset(...) function was misused.
With an initialization such as :
int Color = RGB(255, 0, 0);
for (int i = 0 ; i < 500 * 500 ; i++)
pBuffer[i] = Color;
I get a perfectly blue image as expected.
Related
I'm an absolute beginner at this but have managed to blunder my way to 93% of where I want to be. Need help for the final 7%.
I've manually created a bitmap like so:
BITMAPINFO bmpInfo = { 0 };
BITMAPINFOHEADER bmpInfoHeader = { 0 };
BITMAP ImageBitmap;
void *bits;
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfoHeader.biBitCount = 32;
bmpInfoHeader.biClrImportant = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biCompression = BI_RGB;
bmpInfoHeader.biHeight = -IMAGE_DISPLAY_HEIGHT;
bmpInfoHeader.biWidth = IMAGE_DISPLAY_WIDTH;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biSizeImage = IMAGE_DISPLAY_WIDTH * IMAGE_DISPLAY_HEIGHT * 4;
ZeroMemory(&bmpInfo, sizeof(bmpInfo));
bmpInfo.bmiHeader = bmpInfoHeader;
bmpInfo.bmiColors->rgbBlue = 0;
bmpInfo.bmiColors->rgbGreen = 0;
bmpInfo.bmiColors->rgbRed = 0;
bmpInfo.bmiColors->rgbReserved = 0;
g_hImageBitmap = CreateDIBSection(hDC, &bmpInfo, DIB_RGB_COLORS, &bits, NULL, 0);
GetObject(g_hImageBitmap, sizeof(BITMAP), &ImageBitmap);
for (i = 0; i < IMAGE_DISPLAY_WIDTH; i++) {
for (j = 0; j < IMAGE_DISPLAY_HEIGHT; j++) {
((unsigned char *)bits)[j*IMAGE_DISPLAY_WIDTH * 4 + i * 4] = 255; // Blue
((unsigned char *)bits)[j*IMAGE_DISPLAY_WIDTH * 4 + i * 4 + 1] = 255; // Green
((unsigned char *)bits)[j*IMAGE_DISPLAY_WIDTH * 4 + i * 4 + 2] = 255; // Red
((unsigned char *)bits)[j*IMAGE_DISPLAY_WIDTH * 4 + i * 4 + 3] = 0;
}
}
g_ImageBitmapPixels = bits;
and elsewhere WM_PAINT handles drawing this like so
hdc = BeginPaint(hwnd, &ps);
if (g_hImageBitmap != NULL) {
GetObject(g_hImageBitmap, sizeof(BITMAP), &bm);
hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, g_hImageBitmap);
BitBlt(hdc, UPPER_LEFT_IMAGE_X, UPPER_LEFT_IMAGE_Y,
bm.bmWidth, bm.bmHeight, hMemoryDC, 0, 0, SRCCOPY);
SelectObject(hMemoryDC, hOldBitmap);
}
Given the global variable g_ImageBitmapPixels other parts of the program can change and manipulate individual pixels in the bitmap, and when that happens, I use
InvalidateRect(hwnd, &RECT_ImageUpdate_Window, TRUE);
UpdateWindow(hwnd);
to update just that little portion of the screen. Works great. Hooray for me.
To get to the point, my question is, if a function has ONLY the HBITMAP (g_hImageBitmap) ... is there a way to call the Windows library functions to draw lines, text, circles, filled circles, to the HBITMAP? Like these functions
MoveToEx(hDC, x1, y1, NULL);
LineTo(hDC, x2, y2 );
HBRUSH hRedBrush = CreateSolidBrush(RGB(255, 0, 0));
FillRect(hDC, &somerectangle, hRedBrush);
except instead of needing a device context, they just take the HBITMAP?
I have a pointer to the actual pixels (g_ImageBitmapPixels) so I could just write my own line drawing, circle drawing, rectangle filling functions. Indeed I have done that, but it seems a shame not to use the functions Microsoft so kindly provides. Also, I'm not smart enough to make my own text-drawing functions.
Thank you for your help.
I have several child window on the main window, and some are GDI windows, and some are opengl rendered window, one function is to capture the image with a rect (may cover different combination of windows). This function works fine under windows xp. However, under windows 7, all opengl rendered windows are black. I did some research and someone said that the gdi cannot directly access the frame buffer via the window DC, and has to use glReadPixels to combine the bitmap. This approach however is awkward since I have to combine each window in that rect separately. Anyone has a better option for me?
Here is my code for catching a bmp:
void MainWndClass::catchBmp(const char* path_fn, bool drawAreaOnly /*=0*/)
{
CDC *pDC=GetDC();
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);
int Left,Top,Width,Height;
if (drawAreaOnly)
{
Left = rBDWin.left;
Top = rBDWin.top;
Width = rBDWin.right-rBDWin.left;
Width = Width/4*4;
Height = rBDWin.bottom-rBDWin.top;
Height = Height/4*4;
}
else
{
Left=rbmpWin.left;
Top=rbmpWin.top;
Width=rbmpWin.right-rbmpWin.left;
Width=Width/4*4;
Height=rbmpWin.bottom-rbmpWin.top;
Height=Height/4*4;
}
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap;
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);
//it seems does no work
//short bpp=24;
if(BitPerPixel>24) BitPerPixel=24;
memBitmap.SetBitmapBits(2,&BitPerPixel);
oldmemBitmap = memDC.SelectObject(&memBitmap);
//copy the bitmap from the pDC (source)
memDC.BitBlt(0, 0, Width, Height, pDC, Left, Top, SRCCOPY);
/*
CString title;
GetWindowText(title);
memDC.SetBkMode(TRANSPARENT);
memDC.TextOut(64,4,title);
*/
BITMAP bmp;
memBitmap.GetBitmap(&bmp);
if(bmp.bmBitsPixel>24)
{
bmp.bmBitsPixel=24;
//bmp.bmWidthBytes=bmp.bmWidth*3;
}
bmp.bmWidthBytes=bmp.bmWidth*(bmp.bmBitsPixel/8);
FILE *fp=NULL;
//path_fn+=".bmp";
fp=fopen((LPCTSTR)path_fn,"w+b");
BITMAPINFOHEADER bih = {0};
bih.biBitCount = bmp.bmBitsPixel;
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;
bih.biWidth = bmp.bmWidth;
BITMAPFILEHEADER bfh = {0};
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;
bfh.bfType = (WORD)0x4d42;
if(fp)
{
fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);
fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);
}
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];
//copy the bits to the buffer
int ret=GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);
if(fp)
fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);
delete [] p;
if(fp)
fclose(fp);
memDC.SelectObject(oldmemBitmap);
}
The opengl window is configured as:
PIXELFORMATDESCRIPTOR pixelDesc =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,//
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
I want to emphysis the fact again:
it works under xp, but not under win7 (the opengl window part is black)
Hello I finally got a perfect solution for this. According to information given by Mats Pertersson, and I am pretty sure that is the reason since it matches the facts. Windows 7 introduces the transparent window appearance, and each window is not the final results. Final results (the screen outputs) are composed from all the windows. So I came the solution, capture the final screen instead of capture the main window. And it works perfect under both xp and win 7.
Main changes: all DC comes from the screen instead of the window, hence relating functions are all changed to global gdi functions.
Here is the code:
catchBmp(const char* path_fn, bool drawAreaOnly /*=0*/)
{
//CDC *pDC=GetDC();
HDC hdcScreen;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
hdcScreen=::GetDC(NULL);
int BitPerPixel = ::GetDeviceCaps(hdcScreen,BITSPIXEL);
int Left,Top,Width,Height;
if (drawAreaOnly)
{
Left = rBDWin.left;
Top = rBDWin.top;
Width = rBDWin.right-rBDWin.left;
Width = Width/4*4;
Height = rBDWin.bottom-rBDWin.top;
Height = Height/4*4;
}
else
{
Left=rbmpWin.left;
Top=rbmpWin.top;
Width=rbmpWin.right-rbmpWin.left;
Width=Width/4*4;
Height=rbmpWin.bottom-rbmpWin.top;
Height=Height/4*4;
}
hdcMemDC=::CreateCompatibleDC(hdcScreen);
hbmScreen=::CreateCompatibleBitmap(hdcScreen,Width,Height);
if(BitPerPixel>24) BitPerPixel=24;
::SetBitmapBits(hbmScreen,2,&BitPerPixel);
::SelectObject(hdcMemDC,hbmScreen);
BitBlt(hdcMemDC,
0,0,Width,Height,hdcScreen,Left,Top,SRCCOPY);
::GetObject(hbmScreen,sizeof(BITMAP),&bmpScreen);
if(bmpScreen.bmBitsPixel>24)
{
bmpScreen.bmBitsPixel=24;
}
bmpScreen.bmWidthBytes=bmpScreen.bmWidth*(bmpScreen.bmBitsPixel/8);
FILE *fp=NULL;
fp=fopen((LPCTSTR)path_fn,"w+b");
BITMAPINFOHEADER bih = {0};
bih.biBitCount = bmpScreen.bmBitsPixel;
bih.biCompression = BI_RGB;
bih.biHeight = bmpScreen.bmHeight;
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmpScreen.bmWidthBytes * bmpScreen.bmHeight;
bih.biWidth = bmpScreen.bmWidth;
BITMAPFILEHEADER bfh = {0};
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bfh.bfSize = bfh.bfOffBits + bmpScreen.bmWidthBytes * bmpScreen.bmHeight;
bfh.bfType = (WORD)0x4d42;
if(fp)
{
fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);
fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);
}
byte * p = new byte[bmpScreen.bmWidthBytes * bmpScreen.bmHeight];
GetDIBits(hdcScreen, hbmScreen, 0, Height, p, (LPBITMAPINFO) &bih, DIB_RGB_COLORS);
if(fp)
fwrite(p, 1, bmpScreen.bmWidthBytes * bmpScreen.bmHeight, fp);
delete [] p;
if(fp)
fclose(fp);
::DeleteObject(hbmScreen);
::DeleteObject(hdcMemDC);
::ReleaseDC(NULL,hdcScreen);
//memDC.SelectObject(oldmemBitmap);
}
As a developer working with GPU's on and off (currently "on", but not doing graphigs) for the past ten or so years, I'll try to explain what is going on:
The GPU often have more than one "layer" that it can output to - for example, on modern graphics cards, the mouse cursor lives in a layer of it's own, so that we don't have to redraw things (as used to be the case, the video card/driver would "remember" what was under the mouse-cursor, and the redraw that when you moved the mouse). That layer is on top of the actual graphics on the screen, and they are combined when the frame buffer memory is scanned out - that is, when pixel-colours are sent to the display itself - each layer is read in a defined order, and the colour of the different layers are combined according to their respective alpha-value.
Some OpenGL drivers & hardware find it much easier to draw the 3D to a separate layer, and then combine the two during "scanning out" phase. This sometimes gives better performance, because the GL driver "owns" this layer, and doesn't have to fight with GDI trying to draw to the screen at the same time.
Of course, when GDI reads back the content, it can only read the content that GDI knows about [this is also why the mouse cursor is typically not present in a screen-copy]
I am working on a project that I am trying to design for a class. I want to try and mimic the behavior of Phillips' Abilight or the common homebrew 'BobLight'. It gets the average color of the screen,via screenshot, in my case by averaging the RGB pixel data, pixel by pixel. I have decided to try and do this by using the windows GDI api and so far I am getting some pretty good results, I learned not to use GetPixel() to read pixel data since its so incredibly slow, and my speed is perfectly acceptable so far.
My problem is that when I try to implement it in a loop that can run as long as desired, it eventually hangs giving me an error saying: std::bad_alloc at memory location 0x0027F9C4.
at this line of code:
BYTE *lpbitmap = new BYTE[dwBmpSize];
I attached my entire code below
Does anyone have an idea what could be causing this error? Am I shouldn't be running out of any kind of memory, and I'm really at a loss..
Thanks in advance for any help!
#define _WIN32_WINNT 0x0501 //xp
#include <windows.h>
#include <iostream>
#include <vector>
#include <time.h>
#define NUM_FRAMES 180
using namespace std;
int main()
{
int time_start, time_finish;
float time_total;
time_start = clock();
for(int z = 0; z < NUM_FRAMES; z++)
{
HDC hScreenDC = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
// and a device context to put it in
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
int x = GetDeviceCaps(hScreenDC, HORZRES);
int y = GetDeviceCaps(hScreenDC, VERTRES);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, x, y);
// get a new bitmap
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
RECT desktop;
// Get a handle to the desktop window
const HWND hDesktop = GetDesktopWindow();
// Get the size of screen to the variable desktop
GetWindowRect(hDesktop, &desktop);
// The top left corner will have coordinates (0,0)
// and the bottom right corner will have coordinates
// (horizontal, vertical)
int horizontal = desktop.right;
int vertical = desktop.bottom;
BitBlt(hMemoryDC, 0, 0, horizontal, vertical, hScreenDC, 0, 0, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = horizontal;
bi.biHeight = vertical;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmpSize = ((horizontal * bi.biBitCount + 31) / 32) * 4 * vertical;
// Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented as wrapper functions that
// call HeapAlloc using a handle to the process's default heap. Therefore, GlobalAlloc and LocalAlloc
// have greater overhead than HeapAlloc.
HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
BYTE *lpbitmap = new BYTE[dwBmpSize];
//BITMAP bm;
GetDIBits(hMemoryDC,hBitmap,0,(UINT)vertical,lpbitmap, (BITMAPINFO *)&bi, DIB_RGB_COLORS);
GetObject (hBitmap, sizeof(lpbitmap), &lpbitmap);
unsigned long red = 0, green = 0, blue = 0;
for (int i = 0; i < horizontal; i++)
{
for (int j = 0; j < vertical; j++)
{
blue += lpbitmap[0];
green += lpbitmap[1];
red += lpbitmap[2];
lpbitmap += 4;
}
}
red = red/(horizontal*vertical);
green = green/(horizontal*vertical);
blue = blue/(horizontal*vertical);
//cout << "Red: " << red << " Green: " << green << " Blue: " << blue << endl;
}
time_finish = clock();
time_total = time_finish - time_start;
cout << endl << NUM_FRAMES/((time_total)/10000) << endl;
//release
//DeleteDC(hdc);
//DeleteObject(hbmp);
//ReleaseDC(NULL, hdcScreen);
std::system("pause");
return 0;
}
Your release code appears to be commented out, meaning you're at least leaking hBitmap every frame, which will add up quickly (around 470MB/s to be specific). Actually, it looks like you're leaking lpbitmap as well, so your 180 frames will burn about 2.8 GB on a 1080p display.
i am doing a program where you take a screenshot of a window and then scan every pixel of that picture. But I have a problem assigning RGBQUAD array to the taken screen. Every pixel has the same RGB which is 205. Here is a piece of my code:
RGBQUAD *pixel = malloc((ssWidth * ssHeight)* sizeof(RGBQUAD));
hdcScreen = GetDC(gameHandle);
hdc = CreateCompatibleDC(hdcScreen);
hBmp = CreateCompatibleBitmap(hdcScreen, ssWidth, ssHeight);
SelectObject(hdc, hBmp);
BitBlt(hdc, 0, 0, ssWidth, ssHeight, hdcScreen, xCenter, yCenter, SRCCOPY);
GetDIBits(hdc, hBmp, 0, ssHeight, pixel, &bmpInfo, DIB_RGB_COLORS);
int p = -1;
for(y_var = 0; y_var < ssWidth; y_var++)
{
for(x_var = 0; x_var < ssHeight; x_var++)
{
if(ComparePixel(&pixel[++p]))
{
SetCursorPos(xCenter + x_var + 3, yCenter + y_var + 3);
}
}
}
bool ComparePixel(RGBQUAD *pixel)
{
printf("%d, %d, %d\n"; pixel -> rgbRed, pixel -> rgbGreen, pixel -> rgbBlue);
return false;
}
ComparePixel(RGBQUAD *pixel) function just checks the RGB values. How do i assign the RGBQUAD to the bitmap of the screenshot?
Multiple issues.
The RGBQUAD **pixel = malloc(... and free(*pixel) appear to be the problem. I think you want RGBQUAD *pixel = malloc((ssWidth * ssHeight)* sizeof(RGBQUAD)); (only 1 *)
Suspect the pixels in GetDIBits() s/b pixel.
I think you want y_var = 0; (x_var = 0; also)
ComparePixel() is not defined, but I think you want something closer to if(ComparePixel(pixel[x_var+(y_var*ssWidth)], the_pixel_to_compare_against))
The free(*pixel); s/b _after the 2 for loops and should be free(pixel);
I'm trying to make an auto-cliker for an windows app. It works well, but it's incredibly slow!
I'm currently using the method "getPixel" which reloads an array everytime it's called.
Here is my current code:
hdc = GetDC(HWND_DESKTOP);
bx = GetSystemMetrics(SM_CXSCREEN);
by = GetSystemMetrics(SM_CYSCREEN);
start_bx = (bx/2) - (MAX_WIDTH/2);
start_by = (by/2) - (MAX_HEIGHT/2);
end_bx = (bx/2) + (MAX_WIDTH/2);
end_by = (by/2) + (MAX_HEIGHT/2);
for(y=start_by; y<end_by; y+=10)
{
for(x=start_bx; x<end_bx; x+=10)
{
pixel = GetPixel(*hdc, x, y);
if(pixel==RGB(255, 0, 0))
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Sleep(50);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(25);
}
}
}
So basically, it just scan a range of pixel in the screen and starts a mouse event if it detects a red button.
I know there are other ways to get the pixel color, such as bitblt. But I've made some researches, and I don't understand how I'm supposed to do, in order to scan a color array. I need something which scans screen very fast in order to catch the button.
Could you please help me?
Thanks.
I found a perfect way which is clearly faster than the GetPixel one:
HDC hdc, hdcTemp;
RECT rect;
BYTE* bitPointer;
int x, y;
int red, green, blue, alpha;
while(true)
{
hdc = GetDC(HWND_DESKTOP);
GetWindowRect(hWND_Desktop, &rect);
int MAX_WIDTH = rect.right;
int MAX_HEIGHT = rect.bottom;
hdcTemp = CreateCompatibleDC(hdc);
BITMAPINFO bitmap;
bitmap.bmiHeader.biSize = sizeof(bitmap.bmiHeader);
bitmap.bmiHeader.biWidth = MAX_WIDTH;
bitmap.bmiHeader.biHeight = MAX_HEIGHT;
bitmap.bmiHeader.biPlanes = 1;
bitmap.bmiHeader.biBitCount = 32;
bitmap.bmiHeader.biCompression = BI_RGB;
bitmap.bmiHeader.biSizeImage = MAX_WIDTH * 4 * MAX_HEIGHT;
bitmap.bmiHeader.biClrUsed = 0;
bitmap.bmiHeader.biClrImportant = 0;
HBITMAP hBitmap2 = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
SelectObject(hdcTemp, hBitmap2);
BitBlt(hdcTemp, 0, 0, MAX_WIDTH, MAX_HEIGHT, hdc, 0, 0, SRCCOPY);
for (int i=0; i<(MAX_WIDTH * 4 * MAX_HEIGHT); i+=4)
{
red = (int)bitPointer[i];
green = (int)bitPointer[i+1];
blue = (int)bitPointer[i+2];
alpha = (int)bitPointer[i+3];
x = i / (4 * MAX_HEIGHT);
y = i / (4 * MAX_WIDTH);
if (red == 255 && green == 0 && blue == 0)
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Sleep(50);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(25);
}
}
}
I hope this could help someone else.
The simple answer is that if this is the method you insist on using then there isn't much to optimize. As others have pointed out in comments, you should probably use a different method for locating the area to click. Have a look at using FindWindow, for example.
If you don't want to change your method, then at least sleep your thread for a bit after each complete screen scan.