Screenshot speichern als ppm
-
Hallo cpp'ler,
ich hänge an dem Problem, dass ich einen Screenshot vom derzeitigen Desktop machen und den dann in einem Bild (nämlich einer ppm (p6)) speichern möchte.
Dank vielen lesens bin ich nun hier:
Image Input::getScreenShot(){ // Handle to device context. If this value is NULL, GetDC retrieves the DC for the entire screen. HDC deviceContext = ::GetDC(desktopWindow); // Create BITMAPINFO structure used by CreateDIBSection BITMAPINFO info = {0}; info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); info.bmiHeader.biWidth = screenWidth; info.bmiHeader.biHeight = screenHeight; info.bmiHeader.biPlanes = 1; info.bmiHeader.biBitCount = 32; // A bitmap handle and a pointer its bit data HBITMAP bitmap = 0; RGBQUAD* memory = 0; /* * The CreateDIBSection function creates a DIB that applications can write to directly. * The function gives you a pointer to the location of the bitmap bit values. You can * supply a handle to a file-mapping object that the function will use to create the bitmap, * or you can let the system allocate the memory for the bitmap. */ bitmap = CreateDIBSection(deviceContext, &info, DIB_RGB_COLORS, (void**)&memory,0, 0); //This function releases a device context, freeing it for use by other applications. ReleaseDC(desktopWindow, deviceContext); /** * Blit the contents of the desktop (winDC) to the bitmap (selected in memDC). * * "blit" bedeutet einen (rechteckigen) Block in einem Grafikspeicher an eine andere * Position in diesem Speicher oder manchmal einem andern Speicher zu verschieben. */ HDC winDC = GetWindowDC(desktopWindow); // This function creates a memory device context (DC) compatible with the specified device. HDC memDC = CreateCompatibleDC(winDC); // This function selects an object into a specified device context. SelectObject(memDC, bitmap); /** * This function transfers pixels from a specified source rectangle to a specified destination * rectangle, altering the pixels according to the selected raster operation (ROP) code. */ BitBlt(memDC, 0, 0, screenWidth, screenHeight, winDC, 0, 0, SRCCOPY); // The DeleteDC function deletes the specified device context (DC). //DeleteDC(memDC); //This function releases a device context, freeing it for use by other applications. // ReleaseDC(desktopWindow, winDC); cout<<"P6 1680 1050 255\n"; int offset=0; unsigned int i=screenWidth*screenHeight; for (unsigned int y=1; y<=screenHeight; y++){ offset=(y*screenWidth); for (unsigned int x=0; x<screenWidth; x++){ RGBQUAD *p = &memory[i-offset]; cout<<p->rgbRed<<p->rgbGreen<<p->rgbBlue; offset--; } } // delete bitmap DeleteObject(bitmap); }Wenn ich nun .exe > text.ppm aufrufe (die natürlich die funktion nutzt), bekomme ich zwar ein richtig gedrehtes Bild, aber die die Pixel sind irgendwie.... kaputt...
sample (5mb): http://christoph.mytanet.de/screenshot.ppm
Danke im vorraus.
Christoph
-
Fehler gefunden. Windows speichert für jedes \n ein \r\t und das macht alles kaputt. Also einfach den outputstream auf binary stellen.