打开图象文件:
void CDanwendangDoc::OnFileOpen()
{
LPCTSTR lpszFilter="BMP Files(*.bmp)|*.bmp|任何文件|*.*||";
CFileDialog dlg1(TRUE,lpszFilter,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,lpszFilter,NULL);
CString filename;
CFile file;
//
AfxMessageBox("无法ahhahaa文件!",MB_OK,0);
if(dlg1.DoModal()==IDOK)
{
filename=dlg1.GetPathName();
if(file.Open(filename,CFile::modeRead|CFile::shareDenyNone,NULL)==0)
{
AfxMessageBox("无法打开文件!",MB_OK,0);
return;
}
file.Read(&bf,sizeof(bf));
if(bf.bfType!=0x4d42)//
{
AfxMessageBox("非BMP文件!",MB_OK,0);
return;
}
if(file.GetLength()!=bf.bfSize)
{
AfxMessageBox("文件已损坏!",MB_OK,0);
return;
}
file.Read(&bi,sizeof(bi));
numQuad=0;
if(bi.biBitCount<24)
{
numQuad=1<<bi.biBitCount;
}
pbi=(BITMAPINFO*)HeapAlloc(GetProcessHeap(),0,sizeof(BITMAPINFOHEADER)+numQuad*sizeof(RGBQUAD));
memcpy(pbi,&bi,sizeof(bi));
quad=(RGBQUAD*)((BYTE*)pbi+sizeof(BITMAPINFOHEADER));
if(numQuad!=0)
{
file.Read(quad,sizeof(RGBQUAD)*numQuad);
}
bi.biSizeImage=bf.bfSize-bf.bfOffBits;
lpBuf=(BYTE*)HeapAlloc(GetProcessHeap(),0,bi.biSizeImage);
file.Read(lpBuf,bi.biSizeImage);
file.Close();
flag=1;
}
AfxMessageBox("已经加载图象文件!请单击显示!",MB_OK,0);
}
显示:
void CDanwendangView::OnPaint()
{
CPaintDC dc(this);
CDanwendangDoc *pDoc=GetDocument();
ASSERT_VALID(pDoc);
if(pDoc->flag==1)
{
SetDIBitsToDevice(dc.m_hDC,0,0,pDoc->bi.biWidth,pDoc->bi.biHeight,0,0,0,pDoc->bi.biHeight,pDoc->lpBuf,pDoc->pbi,DIB_RGB_COLORS);
}
Invalidate(FALSE);
}