After a long struggle, I have been searching for information on the Internet and only found the file stored as BMP, but the customer requested to convert it to JPG. However, no ideal solution was found. Too troublesome.
Finally, a solution was found at that moment. The following is a summary:
1. hbitmap copyscreentobitmap (lprect); // Save the specified area of the screen as an imageHbitmap copyscreentobitmap (lprect) // lprect indicates the selected region {HDC hscrdc, hmemdc; // hbitmap, holdbitmap, and int nx, NY, nx2, ny2; // The Int nwidth and nheight coordinates of the selected region; // The bitmap width and height int xscrn and yscrn; // screen resolution // ensure that the selected area is not empty. If (isrectempty (lprect) return NULL; // create the device description table hscrdc = createdc (_ T ("display"), null) for the screen ); // create a compatible memory device description table for the screen device description table hmemdc = createcompatibledc (hscrdc); // obtain the coordinates of the selected area Nx = lprect-> left; NY = lprect-> top; nx2 = lprect-> right; ny2 = lprect-> bottom; // obtain the screen resolution xscrn = getdevicecaps (hscrdc, horzres ); yscrn = getdevicecaps (hscrdc, vertres); // make sure that the selected region is visible if (nx <0)
Nx = 0; If (NY <0)
NY = 0; If (nx2> xscrn) nx2 = xscrn; If (ny2> yscrn) ny2 = yscrn; nwidth = nx2-NX; nheight = ny2-NY; // create a bitmap hbitmap = createcompatiblebitmap (hscrdc, nwidth, nheight) compatible with the on-screen device description table; // select the new bitmap to the holdbitmap = (hbitmap) in the memory device description table) selectObject (hmemdc, hbitmap); // copy the screen device description table to the bitblt (hmemdc, nwidth, nheight, hscrdc, NX, NY, srccopy) in the memory device description table ); // obtain the screen bitmap handle hbitmap = (hbitmap) SelectObject (hmemdc, holdbitmap); // clear deletedc (hscrdc); deletedc (hmemdc ); // return the bitmap handle return hbitmap ;}
2. hbitmap getsrcbit (HDC, lprect rect) // set a member variable m_hbitmap, which is called in ondraw or onpaint and obtained from memory first, unlike the first function, temporarily cut the screen, because if the window is suddenly minimized at the moment, then the range of function 1 is not what we need.Hbitmap getsrcbit (HDC, lprect rect) {HDC hbufdc; hbitmap, hbittemp; // create a device context (HDC) hbufdc = createcompatibledc (HDC ); // create hbitmaphbitmap = createcompatiblebitmap (HDC, ABS (rect-> right-rect-> left), ABS (rect-> bottom-rect-> top )); hbittemp = (hbitmap) SelectObject (hbufdc, hbitmap); // obtain the bitmap buffer stretchblt (hbufdc, 0, 0, ABS (rect-> right-rect-> left ), ABS (rect-> bottom-rect-> top), HDC, rect-> left, rect-> top, ABS (rect-> right-rect-> left ), ABS (rect-> bottom-rect-> top), srccopy); // obtain the final bitmap information hbitmap = (hbitmap) SelectObject (hbufdc, hbittemp ); // release the memory deleteobject (hbittemp);: deletedc (hbufdc); Return hbitmap ;}
3. bool savebmptofile (hbitmap, lpstr lpfilename); // Save the image as a BMP fileBool savebmptofile (hbitmap, lpstr lpfilename) // hbitmap is the screen bitmap handle, and lpfilename is the bitmap file name {HDC; // The Int ibits of the device description table; // The number of bytes occupied by each pixel in the current display resolution word wbitcount; // The number of bytes occupied by each pixel in the bitmap // defines the color palette size, pixel byte size in the bitmap, and bitmap file size, number of bytes written to the file DWORD dwpalettesize = 0, dwbmbitssize, dwdibsize, dwwritten; bitmap
Bitmap; // bitmapfileheader bmfhdr; // bitmapinfoheader Bi;
// Bitmap information header structure lpbitmapinfoheader lpbi;
// Point to the bitmap information header structure handle FH, hdib, hpal; hpalette holdpal = NULL; // defines the file, allocates a memory handle, palette handle // calculate the number of bytes occupied by each pixel in a bitmap file. HDC = createdc (_ T ("display"), 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 <= 24) wbitcount = 24; else wbitcount = 32; // calculate the color 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 = (Bi TMAP. bmwidth * wbitcount + 31)/32) * 4 * bitmap. bmheight; // allocate memory for the bitmap content/* XXXXXXXX calculate the bitmap size decomposition (explain the above statement) xxxxxxxxxxxxxxxxxxxx // The number of bytes occupied by each scan row should be an integer multiple of 4. The specific algorithm is: int biwidth = (bitmap. bmwidth * wbitcount)/32; If (bitmap. bmwidth * wbitcount) % 32) biwidth ++; // not an integer multiple plus 1 biwidth * = 4; // here, the number of bytes for each scan row is calculated. Dwbmbitssize = biwidth * bitmap. bmheight; // obtain the size of xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */hdib = globalalloc (ghnd, signature + 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 getdibits (HDC, hbitmap, 0, (uint) Bitmap Under the color palette. bmheight, (lpstr) lpbi + sizeof (bitmapinfoheader) + dwpalettesize, (bitmapinfo *) lpbi, dib_rgb_colors); // restore if (holdpal) {selectpalette (HDC, holdpal, true); realizepalette (HDC);: releasedc (null, HDC);} // create a bitmap file. FH = createfile (lpcwstr) lpfilename, generic_write, 0, null, create_always, file_attribute_normal | file_flag_sequential_scan, null); If (FH = invalid_handle_value) return false; // sets 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. bytes = (DWORD) sizeof (bitmapfileheader) + (DWORD) sizeof (bitmapinfoheader) + dwpalettesize; // write bitmap file header writefile (FH, (lpstr) & bmfhdr, sizeof (bitmapfileheader ), & dwwritten, null); // write the remaining content of the bitmap file writefile (FH, (lpstr) lpbi, sizeof (bitmapinfoheader) + dwpalettesize + dwbmbitssize, & dwwritten, null ); // clear globalunlock (hdib); globalfree (hdib); closehandle (FH); Return true ;}
4. bool savebitmaptofile (hbitmap, lpstr lpfilename) // This method is simpler with less code,
However, the namespace: Using namespace ATL must be used before use; otherwise, "cimage": Undeclared identifier.
When saving an image, this method determines the storage format based on the input file name,
For example, (m_hbitmap is obtained by function 2 or obtained by processing itself)
Savebitmaptofile (m_hbitmap, (lpstr) _ T ("C: \ temp.bmp"); // Save As BMP
Savebitmaptofile (m_hbitmap, (lpstr) _ T ("C: \ temp.jpg"); // save as JPG
Try it yourself in other formatsBool savebitmaptofile (hbitmap, lpstr lpfilename) {cimage IMG; IMG. attach (hbitmap); hresult = IMG. save (lpcwstr) lpfilename); // other image formats are the same as deleteobject (hbitmap); If (failed (hresult) {return false;} else {return true;} 5. use: when using this function, you can choose one of the above functions, one of which is function 1 or 2, and one of which is function 3 or 4.