/* * Windows GDI screenshot save BMP File */#include "stdio.h" #include "windows.h" void save_bgrdata_to_bmp (void *data, int size, int width , int Height,char *bmppath,int bpp) {Bitmapfileheader bmpheader; Bitmapinfoheader Bmpinfo; FILE *FP = fopen (Bmppath, "wb+"); bmpheader.bftype = 0x4d42; bmpheader.bfreserved1 = 0; Bmpheader.bfreserved2 = 0; bmpheader.bfoffbits = sizeof (Bitmapfileheader) + sizeof (bitmapinfoheader); Bmpheader.bfsize = bmpheader.bfoffbits + width*height*bpp/8; bmpinfo.bisize = sizeof (Bitmapinfoheader); Bmpinfo.biwidth = width; Bmpinfo.biheight =-height; Bmpinfo.biplanes = 1; Bmpinfo.bibitcount = BPP; Bmpinfo.bicompression = Bi_rgb; Bmpinfo.bisizeimage = (width*bpp+31)/32*4*height; Bmpinfo.bixpelspermeter = 100; Bmpinfo.biypelspermeter = 100; bmpinfo.biclrused = 0; bmpinfo.biclrimportant = 0; Fwrite (&bmpheader, sizeof (Bmpheader), 1, FP); Fwrite (&bmpinfo, sizeof (Bmpinfo), 1, FP); Fwrite (data, size, 1, FP); Fclose (FP); } int main (int argc,char *argv) {HWND dthwnD = GetDesktopWindow (); HDC DTHDC = GetDC (dthwnd); int dtwidth = GetSystemMetrics (sm_cxscreen); int dtheight = GetSystemMetrics (Sm_cyscreen); HDC dthdcbuf = CreateCompatibleDC (NULL); Hbitmap hbitmap = CreateCompatibleBitmap (dthdc,dtwidth,dtheight); SelectObject (Dthdcbuf, HBITMAP); BitBlt (dthdcbuf, 0, 0, dtwidth, dtheight, DTHDC, 0, 0, srccopy); int bitsize = dtwidth*dtheight*4; BYTE *bit = new Byte[bitsize]; LONG re = getbitmapbits (Hbitmap, bitsize, bit), if (bit && re) {save_bgrdata_to_bmp (bit, bitsize, Dtwidth, Dtheight , "Gdi.bmp", "n"); free (bit);} DeleteObject (HBITMAP);D Eletedc (DTHDCBUF); ReleaseDC (DTHWND,DTHDC); return 1;}
Gdi_bmp.cpp
Windows GDI save BMP