from RGB pixels to Bitmap file

  • Follow


Hello,

I am new to VC++ and currently using VC++6.0.  I have a big headache on
saving bitmaps.  I understand that this is very easy to experienced
people(but not for me), so I might get some help from the experts here.

Basically I have an array of COLORREF variables.  I can print them easily
using bitblt function in CDC class, but I failed to save them properly into
a bitmap file.  I could not make the right bitmapheader and the infoheader
in particular.  I tried a few examples from codeguru.com and get no where
with the source code, run time errors all the time, it got me very
frustrated.  I am trying to prevent using 3rd party libraries.

Does anyone got any classes or functions that I can use to save my array
into a bitmap?  I will be grateful if you can send me one.  THanks a lot!!

Pedro


0
Reply yipp (1) 7/15/2003 1:55:43 PM

Hi,

Have you tried the sample application DIBLOOK from the MSDN?
It contains some DIBAPI for manipulating DIB.


Pedro Yip wrote:
> Hello,
> 
> I am new to VC++ and currently using VC++6.0.  I have a big headache on
> saving bitmaps.  I understand that this is very easy to experienced
> people(but not for me), so I might get some help from the experts here.
> 
> Basically I have an array of COLORREF variables.  I can print them easily
> using bitblt function in CDC class, but I failed to save them properly into
> a bitmap file.  I could not make the right bitmapheader and the infoheader
> in particular.  I tried a few examples from codeguru.com and get no where
> with the source code, run time errors all the time, it got me very
> frustrated.  I am trying to prevent using 3rd party libraries.
> 
> Does anyone got any classes or functions that I can use to save my array
> into a bitmap?  I will be grateful if you can send me one.  THanks a lot!!
> 
> Pedro
> 
> 

0
Reply davdavdav (16) 7/15/2003 8:37:09 PM


>-----Original Message-----
>Hello,
>
>I am new to VC++ and currently using VC++6.0.  I have a 
big headache on
>saving bitmaps.  I understand that this is very easy to 
experienced
>people(but not for me), so I might get some help from 
the experts here.
>
>Basically I have an array of COLORREF variables.  I can 
print them easily
>using bitblt function in CDC class, but I failed to save 
them properly into
>a bitmap file.  I could not make the right bitmapheader 
and the infoheader
>in particular.  I tried a few examples from codeguru.com 
and get no where
>with the source code, run time errors all the time, it 
got me very
>frustrated.  I am trying to prevent using 3rd party 
libraries.
>
>Does anyone got any classes or functions that I can use 
to save my array
>into a bitmap?  I will be grateful if you can send me 
one.  THanks a lot!!
>
>Pedro
>
>
>.
>

Here is a sample of how to save the current contents of 
the CDC to a bitmap file. Calling GetDC() to get the 
screen display and then saving this to a bitmap file.


void MyClass::SaveBitmapToFile( CString strFileName )
{
	//will convert bitmap from DDB to DIB see DDBToDIB
()
	// See DDBToDIB function for more..
	CPalette pal;

	CDC* pDC = GetDC();
	if( pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
	{
		UINT nSize = sizeof(LOGPALETTE) + (sizeof
(PALETTEENTRY) * 256);
		LOGPALETTE *pLP = (LOGPALETTE *) new BYTE
[nSize];
		pLP->palVersion = 0x300;

		pLP->palNumEntries = 
			GetSystemPaletteEntries( pDC-
>m_hDC, 0, 255, pLP->palPalEntry );

		// Create the palette
		pal.CreatePalette( pLP );

		delete[] pLP;
	}

	HANDLE hDIB=DDBToDIB(*m_pBm,BI_RGB,&pal);
	if(hDIB==NULL)
		return;

	//*************************************
	//This code writes the BMP file
	//opens or create file for writing in binary mode
	if( strFileName == "" )
		strFileName = m_strMainLabel + ".bmp";
	FILE* file = fopen( strFileName,"wb");
	if(!file)
	{
		return;
	}

	BITMAPFILEHEADER hdr;
	LPBITMAPINFOHEADER lpbi;
	lpbi=(LPBITMAPINFOHEADER ) hDIB;
	int nColors= 1 << lpbi->biBitCount;
	hdr.bfType= ((WORD) ('M' << 8) | 'B');
	hdr.bfSize=sizeof(hdr) + GlobalSize(hDIB);
	hdr.bfReserved1=0;
	hdr.bfReserved2=0;
	hdr.bfOffBits = (lpbi->biBitCount>8)?(DWORD) 
(sizeof(hdr) + lpbi->biSize):(DWORD) (sizeof( hdr ) + 
lpbi->biSize +
					nColors * sizeof
(RGBQUAD));

   
	//writing header
	fwrite(&hdr,sizeof(hdr),1, file);
	fwrite(lpbi,GlobalSize(hDIB),1,file);
	BYTE dummy=0;// for 14-th bit        
	
	//special step for 14-th bit 
	fseek(file,13, SEEK_SET);
	fwrite(&dummy,1,1,file);
	fclose(file);	 

}

0
Reply tommy (4) 7/16/2003 3:41:20 PM

2 Replies
159 Views

(page loaded in 0.282 seconds)


Reply: