获取文件信息失败
bool mydlg::LoadImageBuffer(HDC memdc,char *szFileName)
{
BITMAPFILEHEADER bitmapfileheader;
BITMAPINFOHEADER bitmapinfoheader;
BITMAPINFO bitmapinfo;
int file_hand;
OFSTRUCT file_data;
file_hand = OpenFile(szFileName,&file_data,OF_READ);
if (file_hand==-1)
return 0;
_lread(file_hand, &bitmapfileheader,sizeof(BITMAPFILEHEADER));
// test if this is a bitmap file
if (bitmapfileheader.bfType != ((WORD) ('M' << 8) | 'B'))
{
// close the file
_lclose(file_hand);
// return error
return 0;
} // end if
// now we know this is a bitmap, so read in all the sections
// first the bitmap infoheader
// now load the bitmap file header
_lread(file_hand, &bitmapinfoheader, sizeof(BITMAPINFOHEADER));
if (bitmapinfoheader.biWidth%4 != 0) // 宽度不是4的倍数
{
MessageBox("文件宽度不是4的倍数");
_lclose(file_hand);
return 0;
}
if(g_pImgBuffer) // clear it if not null
delete[] g_pImgBuffer;
g_pImgBuffer = new RGB[g_nMapHeight * g_nMapWidth]; // make space
// now load the color palette if there is one
if (bitmapinfoheader.biBitCount != 24)
{
// set bitmapinfo
bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapinfo.bmiHeader.biWidth = g_nMapWidth;
bitmapinfo.bmiHeader.biHeight = g_nMapHeight;
bitmapinfo.bmiHeader.biPlanes = 1;
bitmapinfo.bmiHeader.biBitCount = 24;
bitmapinfo.bmiHeader.biCompression = BI_RGB;
GetDIBits(memdc, g_hBitmap, 0, g_nMapHeight, NULL,
&bitmapinfo, DIB_RGB_COLORS);
GetDIBits(memdc, g_hBitmap, 0, g_nMapHeight, g_pImgBuffer,
&bitmapinfo, DIB_RGB_COLORS);
}
else
{
_llseek(file_hand, bitmapfileheader.bfOffBits,FILE_BEGIN);
_lread(file_hand, g_pImgBuffer, 3*g_nMapHeight*g_nMapWidth);
}
// close the file
_lclose(file_hand);
// return success
return 1;
}
图片已经加载,即szFileName获取正确,可是调试发现bitmapfileheader总是错误的,求教错误原因