//
#ifndef _CBITMAPFILE_H_
#define _CBITMAPFILE_H_
class CBitmapFile : public CGdiObject
{
DECLARE_DYNAMIC(CBitmapFile)
public:
static CBitmapFile* PASCAL FromHandle(HBITMAP hBitmap);
// Constructors
CBitmapFile();
BOOL LoadBitmap(LPCTSTR lpszFileName);
BOOL CreateBitmap(int nWidth, int nHeight, UINT nBitCount, const void* lpBits);
BOOL CreateBitmapIndirect(LPBITMAPINFO lpBitmapInfo, const void* lpBits);
// Attributes
operator HBITMAP() const;
int GetBitmap(BITMAP* pBitMap);
protected:
// Attributes
int GetColorNumber(WORD wBitCount);
public:
// Operations
DWORD SetBitmapBits(DWORD dwCount, const void* lpBits);
DWORD GetBitmapBits(DWORD dwCount, LPVOID lpBits);
// Implementation
public:
virtual ~CBitmapFile();
};
#endif
//
//
//
#include "BitmapFile.h"
#include <memory.h>
IMPLEMENT_DYNAMIC(CBitmapFile,CGdiObject);
CBitmapFile* PASCAL CBitmapFile::FromHandle(HBITMAP hBitmap)
{
return (CBitmapFile*) CGdiObject::FromHandle(hBitmap);
}
CBitmapFile::CBitmapFile()
{
}
BOOL CBitmapFile::LoadBitmap(LPCTSTR lpszFileName)
{
CFile file;
if(!file.Open(lpszFileName,CFile::modeRead|CFile::shareDenyWrite))
{
MessageBox(NULL,"BMP file open error!","warning",MB_OK);
return FALSE;
}
BITMAPFILEHEADER bfhHeader;
file.Read(&bfhHeader,sizeof(BITMAPFILEHEADER));
if(bfhHeader.bfType!=((WORD) ('M'<<8)|'B'))
{
MessageBox(NULL,"The file is not a BMP file!","warning",MB_OK);
return FALSE;
}
if(bfhHeader.bfSize!=file.GetLength())
{
MessageBox(NULL,"The BMP file header error!","warning",MB_OK);
return FALSE;
}
UINT uBmpInfoLen=(UINT) bfhHeader.bfOffBits-sizeof(BITMAPFILEHEADER);
LPBITMAPINFO lpBitmap=(LPBITMAPINFO) new BYTE[uBmpInfoLen];
file.Read((LPVOID) lpBitmap,uBmpInfoLen);
if((* (LPDWORD)(lpBitmap))!=sizeof(BITMAPINFOHEADER))
{
MessageBox(NULL,"The BMP is not Windows 3.0 format!","warning",MB_OK);
return FALSE;
}
DWORD dwBitlen=bfhHeader.bfSize - bfhHeader.bfOffBits;
LPVOID lpBits=new BYTE[dwBitlen];
file.ReadHuge(lpBits,dwBitlen);
file.Close();
BOOL bSuccess=CreateBitmapIndirect(lpBitmap, lpBits);
delete lpBitmap;
delete lpBits;
if(!bSuccess)
return FALSE;
return TRUE;
}
BOOL CBitmapFile::CreateBitmap(int nWidth, int nHeight, UINT nBitCount,
const void* lpSrcBits)
{
ASSERT(nBitCount==1||nBitCount==4||nBitCount==8
||nBitCount==16||nBitCount==24||nBitCount==32);
LPBITMAPINFO lpBitmap;
lpBitmap=(BITMAPINFO*) new BYTE[sizeof(BITMAPINFOHEADER) +
GetColorNumber(nBitCount) * sizeof(RGBQUAD)];
lpBitmap->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
lpBitmap->bmiHeader.biWidth=nWidth;
lpBitmap->bmiHeader.biHeight=nHeight;
lpBitmap->bmiHeader.biBitCount=nBitCount;
lpBitmap->bmiHeader.biPlanes=1;
lpBitmap->bmiHeader.biCompression=BI_RGB;
lpBitmap->bmiHeader.biSizeImage=0;
lpBitmap->bmiHeader.biClrUsed=0;
BOOL bSuccess=CreateBitmapIndirect(lpBitmap, lpSrcBits);
delete lpBitmap;
if(!bSuccess)
return FALSE;
return TRUE;
}
BOOL CBitmapFile::CreateBitmapIndirect(LPBITMAPINFO lpBitmapInfo, const void* lpSrcBits)
{
DeleteObject();
LPVOID lpBits;
CDC *dc=new CDC;
dc->CreateCompatibleDC(NULL);
HBITMAP hBitmap=::CreateDIBSection(dc->m_hDC,lpBitmapInfo,DIB_RGB_COLORS,
&lpBits,NULL,0);
ASSERT(hBitmap!=NULL);
delete dc;
Attach(hBitmap);
BITMAP bmp;
GetBitmap(&bmp);
DWORD dwCount=(DWORD) bmp.bmWidthBytes * bmp.bmHeight;
if(SetBitmapBits(dwCount,lpSrcBits)!=dwCount)
{
MessageBox(NULL,"DIB build error!","warning",MB_OK);
return FALSE;
}
return TRUE;
}
CBitmapFile::operator HBITMAP() const
{
return (HBITMAP)(this == NULL ? NULL : m_hObject);
}
int CBitmapFile::GetBitmap(BITMAP* pBitMap)
{
ASSERT(m_hObject != NULL);
return ::GetObject(m_hObject, sizeof(BITMAP), pBitMap);
}
int CBitmapFile::GetColorNumber(WORD wBitCount)
{
ASSERT(wBitCount==1||wBitCount==4||wBitCount==8
||wBitCount==16||wBitCount==24||wBitCount==32);
switch(wBitCount)
{
case 1:
return 2;
case 4:
return 16;
case 8:
return 256;
default:
return 0;
}
}
DWORD CBitmapFile::SetBitmapBits(DWORD dwCount, const void* lpBits)
{
if(lpBits!=NULL)
{
BITMAP bmp;