c - Screen capture code produces black bitmap -


I need to add the ability to take a full screen screenshot, not just the current window. The following code produces a BMP file with the correct dimension, but the image is completely black, what am I doing wrong?

  Zero capture screen (LPCTRT LPS filePathName) {bitmapfileadder BMF header; BITMAPINFO * pbminfo; HBITMAP HBMP; File * oophile; HDC screen; HDC memedicine; Int sHeight; Int sWidth; LPBET Pibof; Bitmap bmp; Word cClrBits; RECT RCLTient; Screen = GetDC (0); MemDC = CreateCompatibleDC (screen); SHeight = GetDeviceCaps (Screen, VERTRES); SWidth = GetDeviceCaps (screen, HORZRES); // GetObject (screen, size (BITMAP), and BMP); HBmp = CreateCompatibleBitmap (screen, sWidth, sHeight); Retrieve // ​​bitmap color format, width, and height GetObject (HBMP, size (BITMAP), (LPTR) and BMP); Convert the color format to // bits count. CClrBits = (WORD) (bmp.bmPlanes * bmp.bmBitsPixel); If (cClrBits == 1) cClrBits = 1; Other if (CLRBIT BBHder.BCs = Psychopharm (BITMAPINFAAD); PBMMFA-> BMIHDitter.BBidath = BMP.BMWDath; PBMMFA-> BMI Header. BHEEET = BMPBHHIT; PBMMFA-> BMI Header. BPNS = BMPBM Plan; PBMMF -> BMI header .biBitCount = bmp.bmBitsPixel, if (cClrBits bmiHeader.biClrUsed = (1bmiHeader.biCompression = BI_RGB; // number of bytes in the array to compute color // indices in biSizeImage Inam store // width should DWORD.. // set to 0 biClrImportant for; until bitmap combine RLE // compressed pbminfo-> bmiHeader.biSizeImage = ((pbminfo-> bmiHeader.biWidth * cClrBits +31) and ~ 31) / 8 * pbminfo-> bmiHeader.biHeight that demonstrates // device key all colors pbminfo-> bmiHeader.biClrImportant = 0 ;. CreateBMPFile (lpszFilePathName, pbminfo, hBmp, memDC);} void CreateBMPFile (LPTSTR pszFile, PBITMAPINFO PBI, HBITMAP hBMP, HDC HDC) {handle HF; // file handle BITMAPFILEHEADER HDR; // bitmap file-title PBITMAPINFOHEADER pbih; // bitmap information-titles LPBYTE lpBits; // Memory Pointer DWORD dwTotal; Total count of // bytes DWORD cb; Incremental number of // bytes BYTE * hp; // byte pointer DWORD dwTmp; Int line; Pbih = (PBITMAPINFOHEADER) PBI; LPBits = (LPBETE) Global Olocco (GMEMFX, PBIH-> Biosizeimage); // color table DIB (RGBQUAD array) and bits // (array of palette index) Retrieve lines = Getbaibits (HDC, Acbimpi, 0, (Word) Pibiaiac-> Bayhait, Elpibits PBI, Diaibi_arjibiaarjiaeloaraars); // Create a .BMP file. Hf = CreateFile (pszFile, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, Zero, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (handle) NULL); Hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "calculate M" // size of the entire file Hdr.bfSize = (DWORD) (Akarf (BITMAPFILEHEADER) + pbih-> biSize + pbih-> biClrUsed * sizeof (RGBQUAD) + pbih -> biSizeImage); Hdr.bfReserved1 = 0; Hdr.bfReserved2 = 0; // Calculate the offset of the color index Hdr.bfOffBits = (DWORD) size (BITMAPFILEHEADER) + pbih-> biSize + pbih-> biClrUsed * sizeof (RGBQUAD); // Copy BITMAPFILEHEADER to BMP file. Listfile (HF, (LPVOID) and HDR, size (bitmapfileadder), (LPDDORD) and DUTTMP, faucet); Copy the BITMAPINFOHEADER and RGBQUAD array into the // file. WriteFile (hf, (LPVOID) pbih, sizeof (BITMAPINFOHEADER) + pbih-> biClrUsed * sizeof (RGBQUAD), (LPDWORD) and dwTmp, (zero)); // Copy the array of color index to BMP file. DwTotal = cb = pbih-> biSizeImage; Hp = lpBits; LifaFile (HF, (LPTR) HP, (Int) CB, (LPDDORD) and DVTMP, Faucet); // Close the .mp file CloseHandle (HF); // free memory. GlobalFree ((HGLOBAL) lpBits); } 

All capture screen functions make a corresponding bitmap equal to the screen and Then write it to disk, so that you can see the default content of bitmap (black), to capture the screen, you need to create a compatible DC, select the bitmap you made in it and blur it from one DC to the other. After this you have to deselect your target bitmap before starting to write the file.


Comments