Saves a byte array as a 24-bit BMP

Source: Internet
Author: User

Directly on the source code:

#include"stdafx.h"#include<iostream>#include<windows.h>using namespacestd;classcsavebytetobmp{ Public:    BOOLSavedib2bmp (intFileNum,Constcstring& Bmpfilename,intIwidth,intIheight, BYTE *pbuffer); voidConstructbih (intNwidth,intNheight, bitmapinfoheader&BiH); voidCONTRUCTBHH (intNwidth,intnheight, bitmapfileheader& bhh);//add 2010-9-04;protected:Private:};

Then the CPP:

#include"stdafx.h"#include"SaveByteToBmp.h"//save buffer to BMP fileBOOLCsavebytetobmp::savedib2bmp (intFileNum,Constcstring& Bmpfilename,intIwidth,intIheight, BYTE *pbuffer)    {Bitmapinfoheader BiH;    Constructbih (iwidth, Iheight, BiH);    Bitmapfileheader bhh;    CONTRUCTBHH (iwidth, iheight, BHH); intWidthstep = (((iwidth * -) + to) & (~ to)) /8;//the actual size of each row occupied (each row is populated with a 4-byte boundary)    intDibsize = Widthstep * iheight;//the size of the buffer (in bytes)CFile file; Try    {        if(file. Open (Bmpfilename, Cfile::modewrite |cfile::modecreate)) {//Write Filefile. Write ((LPSTR)&AMP;BHH,sizeof(Bitmapfileheader)); File. Write ((LPSTR)&bih,sizeof(Bitmapinfoheader)); File.            Write (pbuffer, dibsize); File.            Close (); return true; }    }    Catch (...) {MessageBox (NULL, _t ("csavebytetobmp::savedib2bmp"), _t ("Tips"), mb_iconerror); }    return false;}//build BMP Bitmap File headervoidCSAVEBYTETOBMP::CONTRUCTBHH (intNwidth,intnheight, bitmapfileheader& bhh)//Add 2010-9-04{    intWidthstep = (((nwidth * -) + to) & (~ to)) /8;//the actual size of each row occupied (each row is populated with a 4-byte boundary)Bhh.bftype = ((WORD) ('M'<<8) |'B');//' BM 'Bhh.bfsize = (DWORD)sizeof(Bitmapfileheader) + (DWORD)sizeof(bitmapinfoheader) +widthstep *nheight; Bhh.bfreserved1=0; Bhh.bfreserved2=0; Bhh.bfoffbits= (DWORD)sizeof(Bitmapfileheader) + (DWORD)sizeof(Bitmapinfoheader);}//build BMP File Information headervoidCsavebytetobmp::constructbih (intNwidth,intNheight, bitmapinfoheader&BiH) {    intWidthstep = (((nwidth * -) + to) & (~ to)) /8; Bih.bisize= +;//Header SizeBih.biwidth =nwidth; Bih.biheight=nheight; Bih.biplanes=1; Bih.bibitcount= -;//RGB encoded, bitBih.bicompression = Bi_rgb;//No compression non-compressionBih.bisizeimage = Widthstep*nheight *3; Bih.bixpelspermeter=0; Bih.biypelspermeter=0; Bih.biclrused=0; Bih.biclrimportant=0;}

Saves a byte array as a 24-bit BMP

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.