Hi All,
I'm trying to develop an application that saves a pixel information as bitmap file using VS2008 ( vc++ ). Currently, i have a byte buffer containing pixel data, which is in 1 bit per pixel format. The width and column of the image is 512 x 512. I used Lockbits() method to create the bitmap data and used Bitmap::Save() method to save it in a bmp file. But the problem is that, from the bitmap it seems that some data has been lost and image is not clear.
Could you please help me to create a 1bpp Bitmap ?
The code i used is:
<br />
int nNumComps = pstImageInfo_i->usBitsAllocated;<br />
short sSamplesPerPixel = 1;<br />
int nWidth = pstImageInfo_i->usColumns;<br />
int nHeight = pstImageInfo_i->usRows; <br />
int nFormat = PixelFormat1bppIndexed;<br />
int nNumColors = 2;<br />
ColorPalette* pPalette = 0;<br />
pPalette = reinterpret_cast< ColorPalette* >( malloc( sizeof( ColorPalette ) + <br />
( nNumColors - 1 )<br />
* sizeof( ARGB )));<br />
if( 0 != pPalette )<br />
{<br />
pPalette->Flags = PaletteFlagsGrayScale;<br />
pPalette->Count = nNumColors;<br />
Color clr( 255, 0, 0, 0 );<br />
pPalette->Entries[0] = clr.GetValue();<br />
Color clrWhite( 255, 255, 255, 255 );<br />
pPalette->Entries[1] = clrWhite.GetValue();<br />
}<br />
Bitmap bmpOverlay( nWidth, nHeight, nFormat );<br />
BitmapData btmpData;<br />
ZeroMemory( &btmpData, sizeof( btmpData ));<br />
Rect BMPRect( 0, 0, nWidth, nHeight );<br />
bmpOverlay.LockBits( &BMPRect, ImageLockModeWrite,<br />
nFormat, &btmpData );<br />
int nSize = pstImageInfo_i->dwBufferSize;<br />
if( pstImageInfo_i->usFramesInOverlay > 1 )<br />
{<br />
nSize = nSize / pstImageInfo_i->usFramesInOverlay;<br />
}<br />
memcpy( btmpData.Scan0, pstImageInfo_i->pbData,<br />
nSize );<br />
bmpOverlay.UnlockBits( &btmpData );<br />
Status ReturnStatus = bmpOverlay.RotateFlip( RotateNoneFlipY );<br />
if( Ok == ReturnStatus )<br />
{<br />
if( pPalette )<br />
{<br />
ReturnStatus = bmpOverlay.SetPalette( pPalette );<br />
delete pPalette;<br />
pPalette = 0;<br />
}<br />
}<br />
CLSID bmpClsid;<br />
GetEncoderClsid(L"image/bmp", &bmpClsid);<br />
bmpOverlay.Save( _T( "ImageOverlay.bmp" ), &bmpClsid, NULL );<br />
Thanks in Advance,
Aneev