References: http://bbs.csdn.net/topics/330154355
#include "stdio.h" #include "windows.h"/************************************************************************/ /* Hbitmap for just the screen bitmap handle/* lpFileName for the bitmap file name you need to save/**** /int Savebitmaptofile (Hbitmap hbitmap,lpstr lpFileName) {HDC hdc;//Device Description Table int ibits;//The number of bytes per pixel in the current display resolution WORD WBi tcount;//the number of bytes per pixel in a bitmap DWORD dwpalettesize=0;//define the palette size DWORD dwbmbitssize;//bitmap in pixel byte size DWORD dwdibsize;//bitmap File size DWORD dwwritten;//Write file byte count BITMAP bitmap;//bitmap structure Bitmapfileheader BMFHDR; Bitmap attribute structure Bitmapinfoheader bi; Bitmap file header structure Lpbitmapinfoheader LPBI; Bitmap information header structure pointing to bitmap information header structure HANDLE fh;//definition file handle HANDLE hdib;//allocating memory handle HANDLE hpal;//allocating memory Handle HANDLE holdpal=null;//palette handle//Compute bytes per pixel for bitmap filesNumber of HDC = CreateDC ("DISPLAY", null,null,null); Ibits = GetDeviceCaps (hdc, Bitspixel) * GetDeviceCaps (hdc, PLANES); DeleteDC (HDC); if (ibits <= 1) wbitcount = 1; else if (ibits <= 4) Wbitcount = 4; else if (ibits <= 8) Wbitcount = 8; else if (ibits <=) Wbitcount = 24; else if (ibits<=32) Wbitcount = 24; Calculate palette Size if (wbitcount <= 8) dwpalettesize = (1 << wbitcount) *sizeof (Rgbquad); Set the bitmap information header structure GetObject (hbitmap, sizeof (BITMAP), (LPSTR) &bitmap); bi.bisize = sizeof (Bitmapinfoheader); Bi.biwidth = Bitmap.bmwidth; Bi.biheight = Bitmap.bmheight; Bi.biplanes = 1; Bi.bibitcount = Wbitcount; Bi.bicompression = Bi_rgb; bi.bisizeimage = 0; Bi.bixpelspermeter = 0; Bi.biypelspermeter = 0; bi.biclrused = 0; bi.biclrimportant = 0; Dwbmbitssize = ((bitmap.bmwidth *wbitcount+31)/32) * 4*bitmap.bmheight; Allocates memory for bitmap content Hdib = GlobalAlloc (ghnd,dwbmbitssize+dwpalettesize+sizeof (Bitmapinfoheader)); LPBI = (lpbitmapinfoheader) globallock (HDIB); if (lpbi==null) {return 0; } *LPBI = bi; Processing palette hPal = Getstockobject (Default_palette); if (hPal) {HDC = GetDC (NULL); Holdpal =:: SelectPalette (HDC, (Hpalette) HPal, FALSE); RealizePalette (HDC); }//Gets the new pixel value under the palette getdibits (HDC, Hbitmap, 0, (UINT) Bitmap.bmheight, (LPSTR) LPBI + sizeof (Bitmap Infoheader) +dwpalettesize, (Lpbitmapinfo) LPBI, dib_rgb_colors); Restore palette if (holdpal) {SelectPalette (HDC, (Hpalette) Holdpal, TRUE); RealizePalette (HDC); ReleaseDC (NULL, HDC); }//Create bitmap fileFH = CreateFile (lpfilename, generic_write, 0, NULL, create_always, File_attribute_normal | File_flag_sequential_scan, NULL); if (fh = = INVALID_HANDLE_VALUE) return FALSE; Set Bitmap file Header Bmfhdr.bftype = 0x4d42; "BM" dwdibsize = sizeof (Bitmapfileheader) + sizeof (Bitmapinfoheader) + dwpalettesize + dwbmbitssize; Bmfhdr.bfsize = dwdibsize; bmfhdr.bfreserved1 = 0; Bmfhdr.bfreserved2 = 0; Bmfhdr.bfoffbits = (DWORD) sizeof (Bitmapfileheader) + (DWORD) sizeof (Bitmapinfoheader) + dwpalettesize; Write Bitmap file header WriteFile (FH, (LPSTR) &bmfhdr, sizeof (Bitmapfileheader), &dwwritten, NULL); Write the remaining contents of the bitmap file WriteFile (FH, (LPSTR) LPBI, Dwdibsize, &dwwritten, NULL); Clear GlobalUnlock (HDIB); GlobalFree (HDIB); CloseHandle (FH); return 1;} Hbitmap getcapturebmp () {HDC hdc; HDC MEMDC; Byte* Data; Hbitmap hbmp; Bitmapinfo bi; memset (&bi, 0, sizeof (BI)); bi.bmiHeader.biSize = sizeof (BITMAPINFO); Bi.bmiHeader.biWidth = GetSystemMetrics (Sm_cxscreen); Bi.bmiHeader.biHeight = GetSystemMetrics (Sm_cyscreen); Bi.bmiHeader.biPlanes = 1; Bi.bmiHeader.biBitCount = 24; HDC = GetDC (NULL); MEMDC = CreateCompatibleDC (HDC); hbmp = CreateDIBSection (MEMDC, &bi, Dib_rgb_colors, (void**) &data, NULL, 0); SelectObject (MEMDC, hbmp); BitBlt (MEMDC, 0, 0, bi.bmiHeader.biWidth, bi.bmiheader.biheight,hdc, 0, 0, srccopy); ReleaseDC (NULL, HDC); DeleteDC (MEMDC); return hbmp; } void Main () {hbitmap hbmp; hbmp = Getcapturebmp (); Savebitmaptofile (hbmp, "c:\\11.bmp"); }
Windows API Implementation