使用c ++和GDI分析屏幕截图以获得屏幕的平均颜色

时间:2013-12-04 00:19:33

标签: c++ bitmap screenshot gdi pixels

我正在研究一个我正在为一个班级设计的项目。我想尝试模仿菲利普斯的“深渊”或常见的自制“BobLight”的行为。它通过屏幕截图得到屏幕的平均颜色,在我的例子中,逐像素地平均RGB像素数据。我决定尝试通过使用Windows GDI api来做到这一点,到目前为止我得到了一些相当不错的结果,我学会了不使用GetPixel()来读取像素数据,因为它非常慢,而且我的速度完全可以接受所以远。

我的问题是,当我尝试在一个可以运行所需的循环中实现它时,它最终会挂起,给出一个错误说:std :: bad_alloc在内存位置0x0027F9C4。

在这行代码中:

BYTE *lpbitmap = new BYTE[dwBmpSize]; 

我在下面附上了我的整个代码 有谁知道可能导致此错误的原因是什么?我不应该没有任何记忆,我真的不知所措。 在此先感谢您的帮助!

#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;
}

1 个答案:

答案 0 :(得分:0)

您的发布代码似乎已被注释掉,这意味着您每帧至少会泄漏hBitmap,这会快速累加(特定时间约为470MB / s)。实际上,看起来你也在泄漏lpbitmap,因此你的180帧将在1080p显示器上刻录大约2.8 GB。