How to save the specified area of ​​the specified DC into hbitmap, and how to save Hbitmap into bitmap

xiaoxiao2021-03-06  41

HbitMap CopyDctobitmap (HDC HSCRDC, LPRECT LPRECT) {

IF (HSCRDC == Null || LPRECT == Null || IsRectempty (LPRECT))

{

AfxMessageBox; "Parameter Error");

Return NULL;

}

HDC HMEMDC; // Screen and Memory Device Description Table Hbitmap Hbitmap, Holdbitmap; // Bit 图 INT NX, NY, NX2, NY2; // Select area coordinate int nwidth, nHEight; // bitmap width and height

/ / Make sure the selected area is not empty rectangular IF (isRectempty (LPRECT)) return null; // Get selected area coordinate nx = lpRect-> LEFT; NY = LPRECT-> TOP; Nx2 = LPRECT-> Right; NY2 = LPRECT-> BOTTOM; NWIDTH = NX2 - NX; NHEIGHT = NY2 - NY; / / To create a compatible memory device description table hmemdc = createcompatibledc (HSCRDC) for the specified device description table; // Create a bit compatible with the specified device description table Figure hbitmap = createcompatibleBitmap (HSCRDC, NWIDTH, NHEIGHT); // Put the new bitmap in the memory device description table HoldbitMap = (hbitmap) SelectObject (HMEMDC, HBitmap); / / copy the screen device Description Table to 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); / / Get the handle of the screen bitmap

Hbitmap = (hbitmap) SelectObject (HMEMDC, Holdbitmap); // Clear DeleteDc (HMEMDC); deleteObject (Holdbitmap); // Return Bitmark Return HbitMap;}

// Save the hbitmap into bitmap Bool SaveBMP (HbitMap Hbitmap, CString FileName) {

IF (hbitmap == null || filename.isempty ())

{

AfxMessageBox; "Parameter Error");

Return False;

}

HDC HDC; // At the current resolution, each pixel accounts measurement INT iBITS; // Bit map accounts for each pixel word wbitcount; // Define palette size, bitmap pixel byte Size, bitmap file size, write file bytes DWord dwpalettesize = 0, dwbmbitstersize = 0, dwdibsize = 0, dwwritten = 0; // bitmap attribute structure Bitmap bitmap; // bitmap file header structure BitmapFileHeader BMFHDR; / / Bitmap information head structure BitmapInfoHeader Bi; // points to bitmap information head structure lpbitmapinfoheader lpbi; // definition file, allocate memory handle, palette handle Handle FH, HDIB, HPAL, HOLDPAL = null; // Computing bitmap file The number of bytes per pixel HDC = Createdc ("Display", NULL, NULL, NULL); IBITS = GetDeviceCaps (HDC, Bitspixel) * getDeDeviceCaps (HDC, Planes); deletedc (HDC); 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.bi.birrimportant = 0; bi.bi.bi.biRused = 0; D wBmBitsSize = ((Bitmap.bmWidth * wBitCount 31) / 32) * 4 * Bitmap.bmHeight; // bitmap memory content distribution hDib = GlobalAlloc (GHND, dwBmBitsSize dwPaletteSize sizeof (BITMAPINFOHEADER)); lpbi = (LPBITMAPINFOHEADER ) Globalock (HDIB); * LPBI = Bi; // Processing palette hpal = getStockObject (default_palette); if (hpal) {hdc = :: getdc (null); Holdpal = :: SelectPalette (HPALETTE) HPAL , False; RealizePalette (HDC);

/ / Get new pixel values ​​under this palette getDibits (HDC, HBitmap, 0, (UINT) Bitmap.Bmheight, (LPSTR) LPBI Sizeof (BitmapInfoHead) DWPALETESIZE, (BitmapInfo *) lpbi, dib_rgb_colors); // Recovery palette if (Holdpal) {:: SelectPalette (HPALETTPALETTE (HDC, (HPALETTE) HOLDPAL, TRUE); RealizePalette (HDC); :: ReleaseDC (NULL, HDC);} // Create 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; // 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 head Writefile (FH, (LPSTR) & BMFHDR, SIZEOF (BitmapfileHeader), & dwwritten, null); // Write the rest of the content Writefile (FH, (LPSTR) LPBI, DWDIBSIZE, & DWWRITEN, NULL ; // Clear GlobalUnlock (HDIB); Globalf Ree (HDIB); CloseHandle (FH);

Return True;}

转载请注明原文地址:https://www.9cbs.com/read-56082.html

New Post(0)