我在写了一个打开一个图象的代码后,运行无错误,但是无法打开bmp格式的图象(只能打开bmp格式的)。
代码如下:
BOOL CShiGaoBanDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
CFile file;
filename = lpszPathName;
if(file.Open(filename, CFile::modeRead, NULL) == 0)
{
AfxMessageBox("无法打开文件!", MB_OK, 0);
return FALSE;
}
file.Read(&bf, sizeof(BITMAPFILEHEADER));
file.Read(&bi, sizeof(BITMAPINFOHEADER));
if(bi.biBitCount != 8)
{
AfxMessageBox("非灰度遥感图片!", MB_OK, 0);
return FALSE;
}
pbi = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) +
256 * sizeof(RGBQUAD));
if(pbi == NULL)
{
AfxMessageBox("内存分配失败!", MB_OK, 0);
return FALSE;
}
memcpy(pbi, &bi, sizeof(BITMAPINFOHEADER));
quad=(RGBQUAD*)((BYTE*)pbi + sizeof(BITMAPINFOHEADER));
file.Read(quad, 256 * sizeof(RGBQUAD));
bi.biSizeImage = bf.bfSize - bf.bfOffBits;
lpOrgBuf = (BYTE*)HeapAlloc(GetProcessHeap(), 0, bi.biSizeImage);
if(lpOrgBuf == NULL)
{
AfxMessageBox("内存分配失败!", MB_OK, 0);
return FALSE;
}
file.Read(lpOrgBuf, bi.biSizeImage);
//给用于显示图象的变量赋值
lShowWidth = bi.biWidth;
lShowHeight = bi.biHeight;
lpShowBuf = (BYTE*)HeapAlloc(GetProcessHeap(), 0, bi.biSizeImage);
if(lpShowBuf == NULL)
{
AfxMessageBox("内存分配失败!");
return FALSE;
}
memcpy(lpShowBuf, lpOrgBuf, bi.biSizeImage);
lpShowPbi = (BITMAPINFO*)HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFOHEADER) +
256 * sizeof(RGBQUAD));
if(lpShowPbi == NULL)
{
AfxMessageBox("内存分配失败!", MB_OK, 0);
return FALSE;
}
memcpy(lpShowPbi, pbi, sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD));
file.Close();
bIsFileOpen = TRUE;
UpdateAllViews(NULL);
return TRUE;
}
请哪位朋友帮个忙,看一下,给点建议!!
QQ:409340554
E_mail:zerozhoutong@yahoo.com.cn