screenshot



  • Hallo,
    Wie kann ich einen Screenshot schnell machen???
    Weil GetPixel in einer for Schleife abzufragen ist zu langsam



  • schonmal versucht "screenshot" in der Forensuche einzutippen...?



  • void doscreen(string filename /*aber bitmap also z.B. test.bmp*/)
    {
    int nWidth = GetSystemMetrics(SM_CXSCREEN);
    	int nHeight = GetSystemMetrics(SM_CYSCREEN);
    
    	HWND hWnd = ::GetDesktopWindow();
    	HDC hdc = ::GetDC(hWnd);
    	HDC memDC = ::CreateCompatibleDC(hdc);
    	HBITMAP hbm = ::CreateCompatibleBitmap(hdc, nWidth, nHeight);
    	HBITMAP hbmOld = (HBITMAP) ::SelectObject(memDC, hbm);
    
    	::BitBlt(memDC, 0, 0, nWidth, nHeight, hdc, 0, 0, SRCCOPY);
    
    	BITMAPINFO bmi;
    
    	ZeroMemory(&bmi, sizeof(bmi));
    
    	bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    	bmi.bmiHeader.biWidth = nWidth;
    	bmi.bmiHeader.biHeight = nHeight;
    	bmi.bmiHeader.biBitCount = 24;
    	bmi.bmiHeader.biPlanes = 1;
    	bmi.bmiHeader.biCompression = BI_RGB;
    	bmi.bmiHeader.biSizeImage = 32 * nWidth * nHeight / 8;
    
    	BYTE *pbBits = new BYTE[bmi.bmiHeader.biSizeImage];
    
    	::GetDIBits(memDC, hbm, 0, bmi.bmiHeader.biHeight, pbBits, &bmi,
    			DIB_RGB_COLORS );
    
    	BITMAPFILEHEADER bfh;
    
    	bfh.bfType = ('M' << 8) + 'B';
    	bfh.bfSize = sizeof(BITMAPFILEHEADER) + bmi.bmiHeader.biSizeImage
    			+ sizeof(BITMAPINFOHEADER);
    	bfh.bfReserved1 = 0;
    	bfh.bfReserved2 = 0;
    	bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    
    	HANDLE hfile = CreateFile(filename.c_str(), GENERIC_WRITE, 0, 0,
    			OPEN_ALWAYS, 0, 0);
    
    	DWORD dwWritten;
    
    	WriteFile(hfile, &bfh, sizeof(bfh), &dwWritten, NULL);
    	WriteFile(hfile, &bmi.bmiHeader, sizeof(BITMAPINFOHEADER), &dwWritten, NULL);
    	WriteFile(hfile, pbBits, bmi.bmiHeader.biSizeImage, &dwWritten, NULL);
    
    	CloseHandle(hfile);
    
    	::SelectObject(memDC, hbmOld);
    	::DeleteDC(memDC);
    	::ReleaseDC(hWnd, hdc);
    	::DeleteObject(hbm);
    
    	delete[] pbBits;
    }
    

Anmelden zum Antworten