Save a part of Bitmap C code

Source: Internet
Author: User
Bool savebmp (char * BMP name, unsigned char * imgbuf, int width, int height, int bibitcount, rgbquad * pcolortable) {// If the bitmap data pointer is 0, no data is transmitted, the function returns if (! Imgbuf) return 0; // color table size, in bytes. The gray image color table // is 1024 bytes. The color image color table size is 0 int colortablesize = 0; if (bibitcount = 1) colortablesize = 8; // int linebyte = (width * bibitcount/8 + 3)/4*4; // open the file * fp = fopen (BMP name, "WB") in binary mode; If (FP = 0) return 0; // apply for bitmap file header Structure Variables, fill in the file header information bitmapfileheader filehead; filehead. bftype = 0x4d42; // BMP Type // bfsize is the sum of the four components of the image file filehead. bfsize = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + colortablesize + linebyte * height; filehead. bfreserved1 = 0; filehead. bfreserved2 = 0; // bfoffbits is the sum of the space required for the first three parts of the image file filehead. bfoffbits = 54 + colortablesize; // write the file header into the file fwrite (& filehead, sizeof (bitmapfileheader), 1, FP); // apply for bitmap information header Structure Variables, fill in the Information header information bitmapinfoheader head; head. bibitcount = bibitcount; head. biclrimportant = 0; head. biclrused = 0; head. bicompression = 0; head. biheight = height; head. biplanes = 1; head. bisize = 40; head. bisizeimage = linebyte * height; head. biwidth = width; head. bixpelspermeter = 0; head. biypelspermeter = 0; // write the bitmap information header into the memory fwrite (& head, sizeof (bitmapinfoheader), 1, FP); // If the grayscale image has a color table, write File if (bibitcount = 1) fwrite (pcolortable, sizeof (rgbquad), 2, FP); // write bitmap data into file fwrite (imgbuf, height * linebyte, 1, FP); // close the file fclose (FP); return 1 ;}

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.