用VC++对话框框架下,浏览bmp图片,读取并显示,本人初学者,请写出详细步骤。
1个回答
展开全部
1,添加按钮打开BMP文件并获取基本数据,在Button对应的函数中添加以下代码:
CFileDialog dlg(TRUE, "*.BMP", NULL, NULL,"位图文件(*.BMP)|*.bmp;*.BMP|");
CString strFileName;
CFile file;
if (dlg.DoModal() == IDOK)
{
strFileName = dlg.GetPathName();
if(!file.Open(strFileName, CFile::modeRead))
return;
BITMAPFILEHEADER bmfHeader;
nFileLen = file.GetLength();
dwDibSize = nFileLen - sizeof(BITMAPFILEHEADER);
if (m_pDib != NULL)
{
delete[] m_pDib;
m_pDib = NULL;
}
m_pDib = new unsigned char[dwDibSize];
if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
return;
if (bmfHeader.bfType != ((WORD)('M' <<8) | 'B'))
return ;
if (file.Read(m_pDib, dwDibSize) != dwDibSize)
return ;
m_bmpInfoHeader = (BITMAPINFOHEADER*)m_pDib;
lHeight = m_bmpInfoHeader->biHeight; //图像长
lWidth = m_bmpInfoHeader->biWidth; //图像宽
lBitCount = m_bmpInfoHeader->biBitCount; //图像位数
switch(lBitCount)
{
case 1:
NumColor = 2;
break;
case 4:
NumColor = 16;
break;
case 8:
NumColor = 256;
break;
case 24:
NumColor = 0;
break;
default:
return;
}
m_pDibBits = m_pDib + sizeof(BITMAPINFOHEADER) + NumColor * sizeof (RGBQUAD);
ShowBMP();
}
2,定义ShowBMP()函数:
CDC *pDC;
CRect rect;
CWnd *pWnd = GetDlgItem(IDC_STATIC_SHOW);
pWnd->GetClientRect(&rect);
pDC = pWnd->GetDC();
SetStretchBltMode(pDC->m_hDC,HALFTONE);//防止自适应窗口图像显示失真
StretchDIBits(pDC->m_hDC,rect.left, rect.top, rect.right, rect.bottom, 0, 0,
lWidth, lHeight, m_pDibBits, (BITMAPINFO*)m_bmpInfoHeader, BI_RGB, SRCCOPY);
3,所需变量定义如下:
BITMAPINFOHEADER *m_bmpInfoHeader;
unsigned char *m_pDib;
unsigned char *m_pDibBits;
DWORD dwDibSize;
DWORD nFileLen;
int lWidth;
int lHeight;
int lBitCount;
int NumColor;
CFileDialog dlg(TRUE, "*.BMP", NULL, NULL,"位图文件(*.BMP)|*.bmp;*.BMP|");
CString strFileName;
CFile file;
if (dlg.DoModal() == IDOK)
{
strFileName = dlg.GetPathName();
if(!file.Open(strFileName, CFile::modeRead))
return;
BITMAPFILEHEADER bmfHeader;
nFileLen = file.GetLength();
dwDibSize = nFileLen - sizeof(BITMAPFILEHEADER);
if (m_pDib != NULL)
{
delete[] m_pDib;
m_pDib = NULL;
}
m_pDib = new unsigned char[dwDibSize];
if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
return;
if (bmfHeader.bfType != ((WORD)('M' <<8) | 'B'))
return ;
if (file.Read(m_pDib, dwDibSize) != dwDibSize)
return ;
m_bmpInfoHeader = (BITMAPINFOHEADER*)m_pDib;
lHeight = m_bmpInfoHeader->biHeight; //图像长
lWidth = m_bmpInfoHeader->biWidth; //图像宽
lBitCount = m_bmpInfoHeader->biBitCount; //图像位数
switch(lBitCount)
{
case 1:
NumColor = 2;
break;
case 4:
NumColor = 16;
break;
case 8:
NumColor = 256;
break;
case 24:
NumColor = 0;
break;
default:
return;
}
m_pDibBits = m_pDib + sizeof(BITMAPINFOHEADER) + NumColor * sizeof (RGBQUAD);
ShowBMP();
}
2,定义ShowBMP()函数:
CDC *pDC;
CRect rect;
CWnd *pWnd = GetDlgItem(IDC_STATIC_SHOW);
pWnd->GetClientRect(&rect);
pDC = pWnd->GetDC();
SetStretchBltMode(pDC->m_hDC,HALFTONE);//防止自适应窗口图像显示失真
StretchDIBits(pDC->m_hDC,rect.left, rect.top, rect.right, rect.bottom, 0, 0,
lWidth, lHeight, m_pDibBits, (BITMAPINFO*)m_bmpInfoHeader, BI_RGB, SRCCOPY);
3,所需变量定义如下:
BITMAPINFOHEADER *m_bmpInfoHeader;
unsigned char *m_pDib;
unsigned char *m_pDibBits;
DWORD dwDibSize;
DWORD nFileLen;
int lWidth;
int lHeight;
int lBitCount;
int NumColor;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询