郁闷,每一次打开位图进程里看都长4M内在,就是不下去,这样会内存用完啊。
帮看看啊。用过的指针我都DELETE了。。郁闷ING...
第一个函数是选择位图
第二个函数是从文件读位图数据(里面有两个全局变量)
第三个函数是显示位图
void CLoadBMPDlg::LoadBMP ()
{
///读入位图
//Use OpenFileDialog to open a bitmap
CFileDialog selectfile(TRUE,"bmp",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"BMP File (*.bmp)|*.bmp|All Files (*.*)|*.*||");
if(selectfile.DoModal ()==IDCANCEL)return;
CString FileName=selectfile.GetPathName ();
//Declar a file class
CFile file;
BITMAPFILEHEADER header;//bitmap head info
bool rval=file.Open (FileName.GetBuffer(0),CFile::modeRead |CFile::typeBinary ,NULL);
if(!rval)
{
MessageBox("Wrong File","Waring",MB_OK);
return;
}
/////////////////////////////
//Read bmp file head data
//a member of header must be "BM" that mean this file is bmp file(value=0x4d42)
/////////////////////////////
file.Read (&header,sizeof(header));
if(header.bfType!=0x4d42)
{
MessageBox("Wrong File","Waring",MB_OK);
file.Close ();
return;
}
/////////////////////////////
//Read bmp datas
////////////////////////////
rval=OpenBMPFile(&file);
if(rval==false)
{
MessageBox("Wrong File Or not 24 bitcount file","Waring",MB_OK);
return;
}
}
BOOL CLoadBMPDlg::OpenBMPFile (CFile *pfile)
{
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
BITMAPINFO* pBMPInfo=NULL;
RGBQUAD* pColorTab=NULL;//Color Table
BYTE* pData=NULL;
DWORD dwSize=0;
pfile->SeekToBegin ();//go to front of file
pfile->Read((void*)&bfh,sizeof(BITMAPFILEHEADER));//read head data
pfile->Read((void*)&bih,sizeof(BITMAPINFOHEADER));//read head info
//////bitmap is 24 bitcout bmpfile
if(bih.biBitCount ==24)
{
pBMPInfo=(BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)];//give info buffer
memcpy(pBMPInfo,&bih,sizeof(BITMAPINFOHEADER));
if(pBMPInfo==NULL)return FALSE;
if(m_pBMPInfo!=NULL)//emputy extern info value
{
m_pBMPInfo=NULL;
delete m_pBMPInfo;
}
if(m_pDIBData!=NULL)//emputy extern bmp data
{
m_pDIBData=NULL;
delete m_pDIBData;
}
//get file size from header
dwSize=bfh.bfSize -bfh.bfOffBits ;
pData=(BYTE*)new char[dwSize];
if(pData==NULL)return FALSE;
//read bmp data
pfile->ReadHuge(pData,dwSize);
//Read Data
m_pBMPInfo=pBMPInfo;
m_pDIBData=(BYTE*)new char[dwSize];
memcpy(m_pDIBData,pData,dwSize);
//free used space
if(pBMPInfo!=NULL)pBMPInfo=NULL;delete pBMPInfo;
if(pData!=NULL)pData=NULL;delete pData;
return TRUE;
}
else
{
///////bitmap is 8 bitcount file
return FALSE;
}
return TRUE;
}
void CLoadBMPDlg::OnLoad()
{
// TODO: Add your control notification handler code here
this->LoadBMP ();
if(m_pBMPInfo==NULL || m_pDIBData==NULL)
return;
HRESULT rval=0;
CPoint point;
COLORREF color=0;
//Read size and bmp data
long bmp_w=m_pBMPInfo->bmiHeader .biWidth ;
long bmp_h=m_pBMPInfo->bmiHeader .biHeight;
USHORT bmp_bits=m_pBMPInfo->bmiHeader .biBitCount ;
//Show map
CClientDC dc(this);
dc.SetStretchBltMode (COLORONCOLOR);
::StretchDIBits(dc.GetSafeHdc (),
0,0,bmp_w,bmp_h,
0,0,bmp_w,bmp_h,
m_pDIBData,m_pBMPInfo,
DIB_RGB_COLORS,SRCCOPY);
}