本文主要是介绍MFC界面上显示BMP图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- void ShowBMPDlg::OnButtonSelectiamge()
- {
- char szFilter[] = "BMP Files (*.bmp)|*.bmp|All Files(*.*)|*.*||";
- CFileDialog dlg( TRUE,"BMP",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter );
- if(dlg.DoModal() == IDOK)
- {
- strPathName = dlg.GetPathName();
- LoadShowBMPFile(strPathName);
- m_hBitmap = BufferToHBITMAP();
- Invalidate();
- }
- }
- BOOL ShowBMPDlg::LoadShowBMPFile(const char *pPathname)
- {
- CFile file;
- if( !file.Open( pPathname, CFile::modeRead) )
- return FALSE;
- DWORD m_nFileLen;
- m_nFileLen = file.GetLength();
- m_pBMPBuffer = new char[m_nFileLen + 1];
- if(!m_pBMPBuffer)
- return FALSE;
- if(file.Read(m_pBMPBuffer,m_nFileLen) != m_nFileLen)
- return FALSE;
- return TRUE;
- }
- HBITMAP ShowBMPDlg::BufferToHBITMAP()
- {
- HBITMAP hShowBMP;
- LPSTR hDIB,lpBuffer = m_pBMPBuffer;
- LPVOID lpDIBBits;
- BITMAPFILEHEADER bmfHeader;
- DWORD bmfHeaderLen;
- bmfHeaderLen = sizeof(bmfHeader);
- strncpy((LPSTR)&bmfHeader,(LPSTR)lpBuffer,bmfHeaderLen);
- if (bmfHeader.bfType != (*(WORD*)"BM")) return NULL;
- hDIB = lpBuffer + bmfHeaderLen;
- BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ;
- BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
- m_bmW = bmiHeader.biWidth;
- m_bmH = bmiHeader.biHeight;
- lpDIBBits=(lpBuffer)+((BITMAPFILEHEADER *)lpBuffer)->bfOffBits;
- CClientDC dc(this);
- hShowBMP = CreateDIBitmap(dc.m_hDC,&bmiHeader,CBM_INIT,lpDIBBits,
- &bmInfo,DIB_RGB_COLORS);
- return hShowBMP;
- }
- void ShowBMPDlg::DrawImage(int x, int y, CDC *pDC)
- {
- HBITMAP OldBitmap;
- CDC MemDC;
- MemDC.CreateCompatibleDC(pDC);
- OldBitmap=(HBITMAP)MemDC.SelectObject(m_hBitmap);
- CBitmap* pBitmap = CBitmap::FromHandle(m_hBitmap);
- if(m_bmW == 0 || m_bmH == 0)
- return ;
- CRect rect;
- GetClientRect(&rect);
- pDC->BitBlt(x,y,rect.Width(),rect.Height(),&MemDC,0,0,SRCCOPY);
- MemDC.SelectObject(OldBitmap);
- }
这篇关于MFC界面上显示BMP图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!