在vc2005中做在对话框中显示bmp图像的程序,以下是源码:有些大的图像只能显示下面1/3,而用mfc 的CFile 和C语言的FILE都没有问题不知道怎么回事,现在不敢用STL的流了。
//全局变量
BITMAPFILEHEADER header;
BITMAPINFOHEADER infoheader;
ifstream inf;
int iLineByteCnt ;
int m_iImageDataSize;
int m_iImageWidth;
int m_iImageHeight;
int m_iBitsPerPixel;
char* m_pImageData;
char*pDib;
//文件处理函数
void CBMPDlg::OnBnClickedButton1()
{
inf.open("E://yang.bmp");
memset(&header, 0, sizeof(header));
inf.read((char*)&header, sizeof(header));
if(header.bfType != 0x4D42)
return ;
BITMAPINFOHEADER *infoheader;
pDib=new char[ header.bfSize-sizeof(header)];
inf.read(pDib, header.bfSize-sizeof(header));
infoheader=(BITMAPINFOHEADER*)pDib;//关于语法
m_iImageWidth = infoheader->biWidth;
m_iImageHeight = infoheader->biHeight;
m_iBitsPerPixel = infoheader->biBitCount;
iLineByteCnt =(((m_iImageWidth*m_iBitsPerPixel) + 31) >> 5) < < 2;
m_iImageDataSize = header.bfSize-header.bfOffBits;
iLineByteCnt=m_iImageDataSize/m_iImageHeight;
if(m_pImageData) delete []m_pImageData;
m_pImageData =pDib+infoheader->biSize;
inf.close();
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
bmi.bmiHeader.biWidth = m_iImageWidth;
bmi.bmiHeader.biHeight = m_iImageHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = m_iImageDataSize;
StretchDIBits(this->GetDC()->GetSafeHdc(), 0, 0, m_iImageWidth, m_iImageHeight,
0, 0, m_iImageWidth, m_iImageHeight, m_pImageData,&bmi, DIB_RGB_COLORS, SRCCOPY);
}