Creating a DIB section from a BMP file


A DIB section is somewhat like the DIB in that it provides access to the bitmap bits for direct modification by the application. But unlike a DIB handle, a DIB section can be used with most GDI functions, including the BitBlt() and StretchBlt() functions. If you are familiar with the LoadImage() function, you know that this function can be used to create a DIB section from a bitmap file by using the LR_LOADFROMFILE flag. Although the documentation says that this flag is not supported in NT, the truth is that it works only partly on NT. Using the LoadImage() function on NT 4.0 with the LR_LOADFROMFILE flag will create a DIB section but the color information in the DIB section is not proper. The function given below works on NT as well as Windows 95.
// Function prototype with default arguments
HBITMAP LoadDIBSectionFromFile( LPCTSTR lpszFileName, LPVOID *ppvBits=NULL, 
				HANDLE hSection=NULL, DWORD dwOffset=0) ;


// LoadDIBSectionFromFile	- Creates a DIB section from BMP file
// lpszFileName			- Name of the BMP file
// ppvBits			- to receive address of bitmap bits
// hSection		- optional handle to a file mapping object
// dwOffset		- offset to the bitmap bit values within hSection
HBITMAP LoadDIBSectionFromFile( LPCTSTR lpszFileName, LPVOID *ppvBits, 
					HANDLE hSection, DWORD dwOffset) 
{
	LPVOID lpBits;
	CFile file;
	if( !file.Open( lpszFileName, CFile::modeRead) )
		return NULL;
	
	BITMAPFILEHEADER bmfHeader;
	long nFileLen;
	
	nFileLen = file.GetLength();
	
	// Read file header
	if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
		return NULL;
	
	// File type should be 'BM'
	if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B'))
		return NULL;

	BITMAPINFO *pbmInfo;
	pbmInfo = (BITMAPINFO *)::GlobalAlloc(GMEM_FIXED, 
				sizeof(BITMAPINFO) + sizeof(RGBQUAD)*256 );
	if (pbmInfo == NULL)
		return NULL;
	
	// Read the BITMAPINFO
	file.Read( pbmInfo, sizeof(BITMAPINFO) + sizeof(RGBQUAD)*256 );
	
	BITMAPINFO &bmInfo = *pbmInfo ;
	
	HBITMAP hBmp = CreateDIBSection( NULL, pbmInfo, DIB_RGB_COLORS, &lpBits, 
						hSection, dwOffset );

	LPBYTE  lpDIBBits;              // Pointer to DIB bits
	int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed : 
				1 << bmInfo.bmiHeader.biBitCount;
	
	if( bmInfo.bmiHeader.biBitCount > 8 )
		lpDIBBits = (LPBYTE)((LPDWORD)(bmInfo.bmiColors + 
			bmInfo.bmiHeader.biClrUsed) + 
			((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
	else
		lpDIBBits = (LPBYTE)(bmInfo.bmiColors + nColors);

	int nOffset = sizeof(BITMAPFILEHEADER) + (lpDIBBits - (LPBYTE)pbmInfo);
	file.Seek( nOffset, CFile::begin);
	file.ReadHuge((LPSTR)lpBits, nFileLen - nOffset); //bmInfo.biSizeImage ); 

	::GlobalFree(pbmInfo);

	if( ppvBits )
		*ppvBits = lpBits;
	
	return hBmp;
}



Comments

  • Copied adnd pasted, but doesn't work.

    Posted by Legacy on 05/24/2002 12:00am

    Originally posted by: Doesn't Work

    The call to CreateDIBSection() returns NULL under NT4SP6. Comments sure would have helped too. I'll look elsewhere.

    Reply
  • it's perfect

    Posted by Legacy on 04/02/2002 12:00am

    Originally posted by: Rabiakhushbakhat

    great it worked perfect!!!

    Reply
  • a problm with BMP

    Posted by Legacy on 01/28/2002 12:00am

    Originally posted by: softzhou

    i wanted to create a bmp file from a bitmap file which i specified,but under different COlor Modal(16Bit and 24Bit),it's create different file,what can i do?thank you

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Information is data with context. The era of Big Data has begun demonstrating to information security that there is more that can, and must, be done to identify threats, reduce risk, address fraud, and improve compliance monitoring activities by bringing better context to data and thereby creating information for actionable intelligence. This analyst report sets the stage and provides insights into IT and information security practitioners' perceptions of the impediments to, and the solutions necessary for, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds