Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi,
- HBITMAP hBMP, HDC hDC);
- PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp);
- void TakeScreenshot(char* Name)
- {
- HDC hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
- HDC hdcCompatible = CreateCompatibleDC(hdcScreen);
- HBITMAP hbmScreen = CreateCompatibleBitmap(hdcScreen, GetDeviceCaps(hdcScreen, HORZRES), GetDeviceCaps(hdcScreen, VERTRES));
- if(hbmScreen == 0)
- MessageBox(NULL, "hbmScreen", "Error", MB_OK);
- if(!SelectObject(hdcCompatible, hbmScreen))
- MessageBox(NULL, "Compatible Bitmap Selection", "Error", MB_OK);
- if(!BitBlt(hdcCompatible, 0,0, GetDeviceCaps(hdcScreen, HORZRES), GetDeviceCaps(hdcScreen, VERTRES), hdcScreen, 0,0, SRCCOPY))
- MessageBox(NULL, "Screen to Compat Blt Failed", "Error", MB_OK);
- PBITMAPINFO pBitmapInfo = CreateBitmapInfoStruct(hbmScreen);
- CreateBMPFile(NULL, Name, pBitmapInfo, hbmScreen, hdcScreen);
- }
- int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
- {
- return 0;
- }
- PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp)
- {
- BITMAP bmp;
- PBITMAPINFO pbmi;
- WORD cClrBits;
- if(!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
- MessageBox(NULL, "error", "Error", MB_OK);
- cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
- if(cClrBits == 1)
- cClrBits = 1;
- else if(cClrBits <= 4)
- cClrBits = 4;
- else if(cClrBits <= 8)
- cClrBits = 8;
- else if(cClrBits <= 16)
- cClrBits = 16;
- else if(cClrBits <= 24)
- cClrBits = 24;
- else
- cClrBits = 32;
- if (cClrBits != 24)
- pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1<< cClrBits));
- else
- pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));
- pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- pbmi->bmiHeader.biWidth = bmp.bmWidth;
- pbmi->bmiHeader.biHeight = bmp.bmHeight;
- pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
- pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
- if (cClrBits < 24)
- pbmi->bmiHeader.biClrUsed = (1<<cClrBits);
- pbmi->bmiHeader.biCompression = BI_RGB;
- pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits + 31) & ~31) / 8 * pbmi->bmiHeader.biHeight;
- pbmi->bmiHeader.biClrImportant = 0;
- return pbmi;
- }
- void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC)
- {
- HANDLE hf;
- BITMAPFILEHEADER hdr;
- PBITMAPINFOHEADER pbih;
- LPBYTE lpBits;
- DWORD dwTotal;
- DWORD cb;
- BYTE *hp;
- DWORD dwTmp;
- pbih = (PBITMAPINFOHEADER) pbi;
- lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
- if(!lpBits)
- MessageBox(NULL, "GlobalAlloc", "Error", MB_OK);
- if(!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS))
- MessageBox(NULL, "GetDIBits", "Error", MB_OK);
- hf = CreateFile(pszFile, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
- if (hf == INVALID_HANDLE_VALUE)
- MessageBox(NULL, "CreateFile", "Error", MB_OK);
- hdr.bfType = 0x4d42;
- hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage);
- hdr.bfReserved1 = 0;
- hdr.bfReserved2 = 0;
- hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof (RGBQUAD);
- if(!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, NULL))
- MessageBox(NULL, "error", "Error", MB_OK);
- if(!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof (RGBQUAD), (LPDWORD) &dwTmp, ( NULL))) MessageBox(NULL, "error", "Error", MB_OK);
- dwTotal = cb = pbih->biSizeImage;
- hp = lpBits;
- if(!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))
- MessageBox(NULL, "error", "Error", MB_OK);
- if(!CloseHandle(hf))
- MessageBox(NULL, "error", "Error", MB_OK);
- GlobalFree((HGLOBAL)lpBits);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement