Hbitmap copydctobitmap (HDC hscrdc, lprect)
{
If (hscrdc = NULL | lprect = NULL | isrectempty (lprect ))
{
Afxmessagebox ("parameter error ");
Return NULL;
}
HDC hmemdc;
// Screen and memory device description table
Hbitmap, holdbitmap;
// Bitmap handle
Int NX, NY, nx2, ny2;
// Coordinates of the selected region
Int nwidth, nheight;
// Bitmap width and height
// Make sure that the selected area is not empty.
If (isrectempty (lprect ))
Return NULL;
// Obtain the coordinates of the selected region
Nx = lprect-> left;
NY = lprect-> top;
Nx2 = lprect-> right;
Ny2 = lprect-> bottom;
Nwidth = nx2-NX;
Nheight = ny2-NY;
// Create a compatible memory device description table for the specified device description table
Hmemdc = createcompatibledc (hscrdc );
// Create a bitmap compatible with the specified device description table
Hbitmap = createcompatiblebitmap (hscrdc, nwidth, nheight );
// Select the new bitmap to the memory device description table.
Holdbitmap = (hbitmap) SelectObject (hmemdc, hbitmap );
// Copy the screen device description table to the memory device description table.
Stretchblt (hmemdc, 0, 0, nwidth, nheight, hscrdc, NX, NY, nwidth, nheight, srccopy );
// Bitblt (hmemdc, 0, 0, nwidth, nheight, hscrdc, NX, NY, srccopy );
// Obtain the handle of the screen bitmap.
Hbitmap = (hbitmap) SelectObject (hmemdc, holdbitmap );
// Clear
Deletedc (hmemdc );
Deleteobject (holdbitmap );
// Returns the bitmap handle.
Return hbitmap;
}
// Save hbitmap as a bitmap
Bool savebmp (hbitmap, cstring filename)
{
If (hbitmap = NULL | filename. isempty ())
{
Afxmessagebox ("parameter error ");
Return false;
}
HDC;
// The number of bytes per pixel in the current resolution
Int ibits;
// The number of bytes in the bitmap.
Word wbitcount;
// Defines the color palette size, pixel bytes in the bitmap, bitmap file size, and number of written file bytes.
DWORD dwpalettesize = 0, dwbmbitssize = 0, dwdibsize = 0, dwwritten = 0;
// Bitmap attribute Structure
Bitmap bitmap;
// Bitmap file header Structure
Bitmapfileheader bmfhdr;
// Bitmap header Structure
Bitmapinfoheader Bi;
// Point to the bitmap information header Structure
Lpbitmapinfoheader lpbi;
// Defines the file, allocates memory handles, and color palette handles.
Handle FH, hdib, hpal, holdpal = NULL;
// Calculate the number of bytes occupied by each pixel in a bitmap file
HDC = createdc ("display", 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 wbitcount = 24;
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. biclrimportant = 0;
Bi. biclrused = 0;
Dwbmbitssize = (bitmap. bmwidth * wbitcount + 31)/32) * 4 * bitmap. bmheight;
// Allocate memory for bitmap content
Hdib = globalalloc (ghnd, dwbmbitssize + dwpalettesize + sizeof (bitmapinfoheader ));
Lpbi = (lpbitmapinfoheader) globallock (hdib );
* Lpbi = Bi;
// Process the palette
Hpal = getstockobject (default_palette );
If (hpal)
{
HDC =: getdc (null );
Holdpal =: selectpalette (HDC, (hpalette) hpal, false );
Realizepalette (HDC );
}
// Obtain the new pixel value under the color palette
Getdibits (HDC, hbitmap, 0, (uint) bitmap. bmheight, (lpstr) lpbi + sizeof (bitmapinfoheader)
+ Dwpalettesize, (bitmapinfo *) lpbi, dib_rgb_colors );
// Restore the palette
If (holdpal)
{
: Selectpalette (HDC, (hpalette) holdpal, true );
Realizepalette (HDC );
: Releasedc (null, HDC );
}
// Create a bitmap file
FH = createfile (filename, generic_write, 0, null, create_always,
File_attribute_normal | file_flag_sequential_scan, null );
If (FH = invalid_handle_value) return false;
// Set the 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 the 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 true;
}
From: http://blog.csdn.net/bobob/article/details/294759#comments