|
发表于 2020-3-2 04:00:01
|
显示全部楼层
CString StrFilter="位图文件(*.bmp)|*.bmp|所有文件(*.*)|*.*||";
CFileDialog Dlg(TRUE,NULL,NULL,NULL,StrFilter,this);
if(!Dlg.DoModal()==IDOK)
return ;
CString StrFileName;
StrFileName=Dlg.GetPathName();
//BITMAPINFO结构指针
BITMAPINFO* pBmpInfo;
//DIB图像数据指针
BYTE* pBmpData;
CFile MyFile;
if(!MyFile.Open(StrFileName,CFile::modeRead|CFile::typeBinary))
return ;
BITMAPFILEHEADER BmpHeader;
if(MyFile.Read(&BmpHeader,sizeof(BmpHeader))!=sizeof(BmpHeader))
{
AfxMessageBox("读位图文件头出现错误!");
return ;
}
if(BmpHeader.bfType!=0x4d42)
{
AfxMessageBox("不是位图文件!");
return ;
}
BITMAPINFOHEADER BmpInfo;
if(MyFile.Read(&BmpInfo,sizeof(BmpInfo))!=sizeof(BmpInfo))
{
AfxMessageBox("读取位图信息出现错误!");
return ;
}
if(BmpInfo.biBitCount!=24)
{
AfxMessageBox("不是真24色位图,程序暂不支持!");
return ;
}
pBmpInfo=(BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)];
if(!pBmpInfo)
{
AfxMessageBox("内存分配错误!");
return ;
}
memcpy(pBmpInfo,&BmpInfo,sizeof(BITMAPINFOHEADER));
DWORD dataBytes=BmpHeader.bfSize-BmpHeader.bfOffBits;
pBmpData=(BYTE*)new char[dataBytes];
if(!pBmpData)
{
AfxMessageBox("内存分配错误!");
delete pBmpInfo;
return ;
}
if(MyFile.Read(pBmpData,dataBytes)!=dataBytes)
{
AfxMessageBox("读位图数据错误!");
delete pBmpInfo;
delete pBmpData;
return ;
}
MyFile.Close();
CClientDC *pDC=new CClientDC(this);
pDC->SetStretchBltMode(COLORONCOLOR);
StretchDIBits(pDC->GetSafeHdc(),0,0,BmpInfo.biWidth,BmpInfo.biHeight,0,0,
BmpInfo.biWidth,BmpInfo.biHeight,
pBmpData,pBmpInfo,DIB_RGB_COLORS,SRCCOPY); |
|