Click here to Skip to main content
11,708,312 members (73,358 online)
Click here to Skip to main content

Tagged as

Save a 24 bit bitmap's pixel data to File in BMP format

, 24 Mar 2012 CPOL 15.6K 3
Rate this:
Please Sign up or sign in to vote.
This tip shows how to save a 24 bitmap to a file given filename, pixel data, bitmap width and bitmap height

Introduction

This tip is about saving a 24 bit bmp image to a file given the raw bitmap bits containing drawing data in pixels and the width and height of the image.

The code

// szPathName : Specifies the pathname
// lpBits	 : Specifies the bitmap bits
// w	: Specifies the image width
// h	: Specifies the image height

bool SaveImage(char* szPathName, void* lpBits, int w, int h)

{ 

//Create a new file for writing

FILE *pFile = fopen(szPathName, "wb");

if(pFile == NULL)

{ 

return false;

}

BITMAPINFOHEADER BMIH;

BMIH.biSize = sizeof(BITMAPINFOHEADER);

BMIH.biSizeImage = w * h * 3;

// Create the bitmap for this OpenGL context

BMIH.biSize = sizeof(BITMAPINFOHEADER);

BMIH.biWidth = w;

BMIH.biHeight = h;

BMIH.biPlanes = 1;

BMIH.biBitCount = 24;

BMIH.biCompression = BI_RGB;

BMIH.biSizeImage = w * h* 3; 

BITMAPFILEHEADER bmfh;

int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize; 

LONG lImageSize = BMIH.biSizeImage;

LONG lFileSize = nBitsOffset + lImageSize;

bmfh.bfType = 'B'+('M'<<8);

bmfh.bfOffBits = nBitsOffset;

bmfh.bfSize = lFileSize;

bmfh.bfReserved1 = bmfh.bfReserved2 = 0;

//Write the bitmap file header

UINT nWrittenFileHeaderSize = fwrite(&bmfh, 1, 

sizeof(BITMAPFILEHEADER), pFile);

//And then the bitmap info header

UINT nWrittenInfoHeaderSize = fwrite(&BMIH, 

1, sizeof(BITMAPINFOHEADER), pFile);

//Finally, write the image data itself 

//-- the data represents our drawing

UINT nWrittenDIBDataSize = 

fwrite(lpBits, 1, lImageSize, pFile);

fclose(pFile);

 

return true;



}

History

Tip uploaded : 16th March, 2012

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
Your Lord does not like people who waste by extravagance, and only the righteous C s, and does so in increments, and they are of those people who are mindful of what memory and time has in offer and they waste none of it.

You may also be interested in...

Comments and Discussions

 
Question{RQ}Code Project how to hiding text(not text file) within a color image 24bits/pixel Pin
tuancrab10-May-12 18:12
membertuancrab10-May-12 18:12 
AnswerRe: {RQ}Code Project how to hiding text(not text file) within a color image 24bits/pixel Pin
Mukit, Ataul11-May-12 19:13
memberMukit, Ataul11-May-12 19:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 24 Mar 2012
Article Copyright 2012 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid