Windows saves BMP images and windowsbmp Images

Source: Internet
Author: User
Tags bmp image

Windows saves BMP images and windowsbmp Images

Saving BMP images in Windows is quite convenient. You can directly copy the code and use it.

1 void savebmp (uchar * pdata, char * bmp _file, int width, int height) 2 {// The file names of the bmp files to be saved are rgb data, respectively, the image length and width are 3 int size = width * height * 3 * sizeof (char); // each pixel has 3 bytes. 4 // the first part of the bitmap. The file information is 5 BITMAPFILEHEADER bfh; 6 bfh. bfType = (WORD) 0x4d42; // bm 7 bfh. bfSize = size // data size 8 + sizeof (BITMAPFILEHEADER) // first section size 9 + sizeof (BITMAPINFOHEADER) // second section size 10; 11 bfh. bfReserved1 = 0; // Reserved 12 bfh. bfReserved2 = 0; // reserved 13 bfh. bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER); // true Data Location 14 15 // bitmap second part, data information 16 BITMAPINFOHEADER bih; 17 bih. biSize = sizeof (BITMAPINFOHEADER); 18 bih. biWidth = width; 19 bih. biHeight =-height; // BMP image scanning starts from the last point. When the image is displayed, the image is inverted. Therefore, use-height, so that the image is 20 bih. biPlanes = 1; // It is 1. You do not need to change it to 21 bih. biBitCount = 24; 22 bih. biCompression = 0; // No Pressure 23 bih. biSizeImage = size; 24 bih. biXPelsPerMeter = 2835; // pixel: 25 bih per meter. biYPelsPerMeter = 2835; 26 bih. biClrUsed = 0; // used color, 24-Bit 0 27 bih. biClrImportant = 0; // each pixel is important 28 FILE * fp = fopen (BMP _file, "wb"); 29 if (! Fp) return; 30 31 fwrite (& bfh, 8, 1, fp); // the header size is 54 bytes, the first part is 14 bytes, the second part is 40 bytes, so the first part will be filled with 16 itself, directly using sizeof. When opening the image, the premature end-of-file encountered error 32 fwrite (& bfh. bfReserved2, sizeof (bfh. bfReserved2), 1, fp); 33 fwrite (& bfh. bfOffBits, sizeof (bfh. bfOffBits), 1, fp); 34 fwrite (& bih, sizeof (BITMAPINFOHEADER), 1, fp); 35 fwrite (pdata, size, 1, fp ); 36 fclose (fp); 37}

In other environments, you must write your own file header and information prefix structure to use it. For details, refer to this blog.

Related Article

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.