Converting BMP to JPG format

Source: Internet
Author: User
These two functions are generally used in graph capturing. I have provided a detailed comment, very understandable ////////////////////////////////////// ////////////// copyscreentobitmap (lprect) {HDC hscrdc, hmemdc; // screen and memory device description table hbitmap, holdbitmap; // bitmap handle int NX, NY, nx2, ny2; // int nwidth, nheight; // bitmap width and height int xscrn, yscrn; // screen resolution // make sure that the selected area is not an empty rectangle if (isrectempty (lprect) return NULL; // create a device description table for the screen. hscrdc = createdc ("display", null); // The device description for the screen. Table creation compatible memory device description table hmemdc = createcompatibledc (hscrdc); // obtain the coordinates of the selected region 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 area 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 device with screen description Table-compatible bitmap hbitmap = createcompatiblebitmap (hscrdc, nwidth, nheight); // select the new bitmap to the memory device description table holdbitmap = (hbitmap) SelectObject (hmemdc, hbitmap ); // copy the on-screen device description table to the memory device description table bitblt (hmemdc, nwidth, nheight, hscrdc, NX, NY, srccopy ); // obtain the screen bitmap handle hbitmap = (hbitmap) SelectObject (hmemdc, holdbitmap); // clear deletedc (hscrdc); deletedc (hmemdc ); // return the bitmap handle return hbitmap ;} //////////////////////////////////////// /// // bool C Snapshotview: savebitmaptofile (hbitmap, lpcstr lpszfilename) {HDC; // device description table int ibits; // 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 size of the color palette, the size of the pixel bytes in the bitmap, the size of the bitmap file, and the number of bytes written to the file DWORD dwpalettesize = 0, dwbmbitssize, dwdib; bitmap bitmap; // bitmapfileheader bmfhdr; // bitmapinfoheader Bi; // bitmap information header lpbitmapinfoheader lpbi; // point to the bitmap information header structure handle FH, hdib, hpal; hpale Tte holdpal = NULL; // definition file, allocate memory handle, palette handle // calculate the number of bytes occupied by each pixel in the bitmap file 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 <= 24) wbitcount = 24; else wbitcount = 32; // calculate the color palette size if (wbitcount <= 8) dwpalettesize = (1 <wbitcount) * Sizeof (rgbquad); // you can specify the Bitmap header. GetObject (hbitmap, sizeof (Bitmap), (lpstr) & Bitmap), AND 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 * wbitcoun T + 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);} // creates a bitmap file, such as FH = createfile (lpszfilename, 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 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.