Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
draw_bmp_file.txt
Package: draw_bmp_file.zip [view]
Upload User: sdblgkt
Upload Date: 2007-01-03
Package Size: 2k
Code Size: 3k
Category:
Picture Viewer
Development Platform:
Visual C++
- // LoadBMP - Loads a BMP file and creates a logical palette for it.
- // Returns - TRUE for success
- // sBMPFile - Full path of the BMP file
- // phDIB - Pointer to a HGLOBAL variable to hold the loaded bitmap
- // Memory is allocated by this function but should be
- // released by the caller.
- // pPal - Will hold the logical palette
- BOOL LoadBMP( LPCTSTR sBMPFile, HGLOBAL *phDIB, CPalette *pPal )
- {
- CFile file;
- if( !file.Open( sBMPFile, CFile::modeRead) )
- return FALSE;
- BITMAPFILEHEADER bmfHeader;
- long nFileLen;
- nFileLen = file.GetLength();
- // Read file header
- if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
- return FALSE;
- // File type should be 'BM'
- if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B'))
- return FALSE;
- HGLOBAL hDIB = ::GlobalAlloc(GMEM_FIXED, nFileLen);
- if (hDIB == 0)
- return FALSE;
- // Read the remainder of the bitmap file.
- if (file.ReadHuge((LPSTR)hDIB, nFileLen - sizeof(BITMAPFILEHEADER)) !=
- nFileLen - sizeof(BITMAPFILEHEADER) )
- {
- ::GlobalFree(hDIB);
- return FALSE;
- }
- BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
- int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed :
- 1 << bmInfo.bmiHeader.biBitCount;
- // Create the palette
- if( nColors <= 256 )
- {
- UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
- LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
- pLP->palVersion = 0x300;
- pLP->palNumEntries = nColors;
- for( int i=0; i < nColors; i++)
- {
- pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
- pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
- pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
- pLP->palPalEntry[i].peFlags = 0;
- }
- pPal->CreatePalette( pLP );
- delete[] pLP;
- }
- *phDIB = hDIB;
- return TRUE;
- }
- void DrawDIB( CDC* pDC, HGLOBAL hDIB, CPalette *pPal )
- {
- LPVOID lpDIBBits; // Pointer to DIB bits
- BOOL bSuccess=FALSE; // Success/fail flag
- BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
- int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed :
- 1 << bmInfo.bmiHeader.biBitCount;
- if( bmInfo.bmiHeader.biBitCount > 8 )
- lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
- bmInfo.bmiHeader.biClrUsed) +
- ((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
- else
- lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
- if( pPal && (pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE) )
- {
- pDC->SelectPalette(pPal, FALSE);
- pDC->RealizePalette();
- }
- ::SetDIBitsToDevice(pDC->m_hDC, // hDC
- 0, // DestX
- 0, // DestY
- bmInfo.bmiHeader.biWidth, // nDestWidth
- bmInfo.bmiHeader.biHeight, // nDestHeight
- 0, // SrcX
- 0, // SrcY
- 0, // nStartScan
- bmInfo.bmiHeader.biHeight, // nNumScans
- lpDIBBits, // lpBits
- (LPBITMAPINFO)hDIB, // lpBitsInfo
- DIB_RGB_COLORS); // wUsage
- }