Save as BMP image through DC

Source: Internet
Author: User
Tags bmp image
// Hbitmap bitmap handle converted image void writebmpfile (hbitmap, lptstr filename, HDC) {bitmap BMP; pbitmapinfo pbmi; Word cclrbits; handle Hf; // file handle bitmapfileheader HDR; // bitmap file-header pbitmapinfoheader pbih; // bitmap Info-header lpbyte lpbits; // memory pointer DWORD dwtotal; // total count of bytes dword cb; // incremental count of bytes byte * HP; // byte pointer DWORD dwtmp; // create The bitmapinfo header information if (! GetObject (hbitmap, sizeof (Bitmap), (lptstr) & BMP) {return;} // convert the color format to a count of bits. cclrbits = (Word) (BMP. bmplanes * BMP. bmbitspixel); If (cclrbits = 1) cclrbits = 1; else if (cclrbits else cclrbits = 32; // allocate memory for the bitmapinfo structure. if (cclrbits! = 24) pbmi = (pbitmapinfo) localalloc (lptr, sizeof (bitmapinfoheader) + sizeof (rgbquad) * (1 else pbmi = (pbitmapinfo) localalloc (lptr, sizeof (bitmapinfoheader )); // initialize the fields in the bitmapinfo structure. pbmi-> bmiheader. bisize = sizeof (bitmapinfoheader); pbmi-> bmiheader. biwidth = BMP. bmwidth; pbmi-> bmiheader. biheight = BMP. bmheight; pbmi-> bmiheader. biplanes = BMP. bmplanes; pbmi-> bmihead Er. bibitcount = BMP. bmbitspixel; If (cclrbits bmiheader. biclrused = (1 // If the bitmap is not compressed, set the bi_rgb flag. pbmi-> bmiheader. bicompression = bi_rgb; // compute the number of bytes in the array of color // indices and store the result in bisizeimage. pbmi-> bmiheader. bisizeimage = (pbmi-> bmiheader. biwidth + 7)/8 * pbmi-> bmiheader. biheight * cclrbits; // set biclrimportant to 0, I Ndicating that all of the // device colors are important. pbmi-> bmiheader. biclrimportant = 0; // now open file and save the data pbih = (pbitmapinfoheader) pbmi; lpbits = (lpbyte) globalalloc (gmem_fixed, pbih-> bisizeimage); If (! Lpbits) {return;} // retrieve the color table (rgbquad array) and the bits if (! Getdibits (HDC, hbitmap (hbitmap), 0, (Word) pbih-> biheight, lpbits, pbmi, dib_rgb_colors) {return;} // create. BMP file. HF = createfile (filename, generic_read | generic_write, (DWORD) 0, null, create_always, file_attribute_normal, (handle) null); If (HF = invalid_handle_value) {return;} HDR. bftype = 0x4d42; // 0x42 = "B" 0x4d = "M" // compute the size of the entire file. HDR. bfsize = (dwo Rd) (sizeof (bitmapfileheader) + pbih-> bisize + pbih-> biclrused * sizeof (rgbquad) + pbih-> bisizeimage); HDR. bfreserved1 = 0; HDR. bfreserved2 = 0; // compute the offset to the array of color indices. HDR. bfoffbits = (DWORD) sizeof (bitmapfileheader) + pbih-> bisize + pbih-> biclrused * sizeof (rgbquad); // copy the bitmapfileheader into. BMP file. if (! Writefile (HF, (lpvoid) & HDR, sizeof (bitmapfileheader), (lpdword) & dwtmp, null) {return ;}// copy the bitmapinfoheader and rgbquad array into the file. if (! Writefile (HF, (lpvoid) pbih, sizeof (bitmapinfoheader) + pbih-> biclrused * sizeof (rgbquad), (lpdword) & dwtmp, (null) {return ;} // copy the array of color indices into. BMP file. dwtotal = CB = pbih-> bisizeimage; HP = lpbits; If (! Writefile (HF, (lpstr) Hp, (INT) Cb, (lpdword) & dwtmp, null) {return ;}// close the. BMP file. If (! Closehandle (HF) {return;} // free memory. globalfree (hglobal) lpbits );}

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.