1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| #include <windows.h>
//-----------------------------------------------------------------------------
// scr2jpg
//-----------------------------------------------------------------------------
BOOL scr2bmp(LPCTSTR pszFileName)
{
HDC hdcMem;
HDC hdcScr;
HBITMAP hbmMem;
HBITMAP hbmOld;
HANDLE hFile;
BITMAPINFO bmi;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bmih;
LPBYTE pPixels;
DWORD dwTmp;
UINT nScrX = GetSystemMetrics(SM_CXSCREEN);
UINT nScrY = GetSystemMetrics(SM_CYSCREEN);
hdcScr = GetDC(NULL);
hbmMem = CreateCompatibleBitmap(hdcScr, nScrX, nScrY);
hdcMem = CreateCompatibleDC(hdcScr);
hbmOld = (HBITMAP) SelectObject(hdcMem, hbmMem);
if(!BitBlt(hdcMem, 0, 0, nScrX, nScrY, hdcScr, 0, 0, SRCCOPY))
goto Erreur;
/*
if(!StretchBlt(hdcMem, 0, 0, 512, 384, hdcScr, 0, 0, nScrX, nScrY, SRCCOPY))
goto Erreur;
*/
bmih.biSize = sizeof(BITMAPINFOHEADER);
bmih.biWidth = nScrX; // 512;
bmih.biHeight = nScrY; // 384;
bmih.biBitCount = GetDeviceCaps(hdcMem, BITSPIXEL);
bmih.biCompression = BI_RGB;
bmih.biPlanes = 1;
bmih.biSizeImage = 0;
bmih.biXPelsPerMeter = 0;
bmih.biYPelsPerMeter = 0;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
bmi.bmiHeader = bmih;
if(!(pPixels = (LPBYTE) GlobalAlloc(GMEM_FIXED, bmih.biWidth * bmih.biHeight * (bmih.biBitCount / 8))))
goto Erreur;
if(!GetDIBits(hdcMem, hbmMem, 0, (WORD) bmih.biHeight, pPixels, &bmi, DIB_RGB_COLORS))
goto Erreur;
bfh.bfType = 0x4d42;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bfh.bfSize = (DWORD) sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
bmih.biWidth * bmih.biHeight * (bmih.biBitCount / 8);
hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
goto Erreur;
if(!WriteFile(hFile, (LPVOID) &bfh, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, NULL))
goto Erreur;
if(!WriteFile(hFile, (LPVOID) &bmih, sizeof(BITMAPINFOHEADER), (LPDWORD) &dwTmp, NULL))
goto Erreur;
if(!WriteFile(hFile, (LPVOID) pPixels, bmih.biWidth * bmih.biHeight * (bmih.biBitCount / 8), (LPDWORD) &dwTmp, NULL))
goto Erreur;
GlobalFree(pPixels);
CloseHandle(hFile);
SelectObject(hdcMem, hbmOld);
ReleaseDC(NULL, hdcScr);
DeleteDC(hdcMem);
return TRUE;
Erreur:
if(pPixels)
GlobalFree(pPixels);
if(hFile)
{
CloseHandle(hFile);
DeleteFile(pszFileName);
}
SelectObject(hdcMem, hbmOld);
ReleaseDC(NULL, hdcScr);
DeleteDC(hdcMem);
return FALSE;
}
//=============================================================================
// winmain
//=============================================================================
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR lpszArg, int iCmdShow)
{
return (int) scr2bmp("screen.bmp");
} |
Partager