Note that there are some explanatory texts on larger screens.

plurals
  1. POAnalyze Screenshots with c++ and GDI to get average color of screen
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>at this line of code:</p> <pre><code>BYTE *lpbitmap = new BYTE[dwBmpSize]; </code></pre> <p>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!</p> <pre><code>#define _WIN32_WINNT 0x0501 //xp #include &lt;windows.h&gt; #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;time.h&gt; #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 &lt; 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, &amp;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 *)&amp;bi, DIB_RGB_COLORS); GetObject (hBitmap, sizeof(lpbitmap), &amp;lpbitmap); unsigned long red = 0, green = 0, blue = 0; for (int i = 0; i &lt; horizontal; i++) { for (int j = 0; j &lt; 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 &lt;&lt; "Red: " &lt;&lt; red &lt;&lt; " Green: " &lt;&lt; green &lt;&lt; " Blue: " &lt;&lt; blue &lt;&lt; endl; } time_finish = clock(); time_total = time_finish - time_start; cout &lt;&lt; endl &lt;&lt; NUM_FRAMES/((time_total)/10000) &lt;&lt; endl; //release //DeleteDC(hdc); //DeleteObject(hbmp); //ReleaseDC(NULL, hdcScreen); std::system("pause"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload