C++ 如何使用MFC从文件加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3029780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to load image from file using MFC
提问by Sweety Khan
My browse button code is
我的浏览按钮代码是
void CFileOpenDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE);
int result=dlg.DoModal();
if(result==IDOK)
{
path=dlg.GetPathName();
UpdateData(FALSE);
}
}
and this is the code for loading an image from resource but that does not work for loading an image from file. i know LoadImage();
is used for this but how? how can i edit this code to load image from file. Plzz help.....
这是从资源加载图像的代码,但不适用于从文件加载图像。我知道LoadImage();
用于此但如何?我如何编辑此代码以从文件加载图像。请帮忙.....
void CFileOpenDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
CRect r;
CBitmap* m_bitmap;
CDC dc, *pDC;
BITMAP bmp;
m_bitmap = new CBitmap();
m_bitmap->LoadBitmapW(IDB_BITMAP1);
m_bitmap->GetBitmap(&bmp);
pDC = this->GetDC();
dc.CreateCompatibleDC(pDC);
dc.SelectObject(m_bitmap);
pDC->BitBlt(200, 200, bmp.bmWidth, bmp.bmHeight, &dc,0 , 0, SRCCOPY);
m_bitmap->DeleteObject();
m_bitmap->Detach();
}
回答by renick
HANDLE hBitMap = ::LoadImage(0, "c:\mybmp.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
CBitmap bmp;
bmp.Attach((HBITMAP)hBitMap);
回答by prompt
If you want to open .JPG, .PNG ... eventually you can use CImage (is a shared class between MFC and ATL)
如果你想打开 .JPG, .PNG ... 最终你可以使用 CImage(是 MFC 和 ATL 之间的共享类)
CImage image;
image.Load ( "picture.jpg" );
image.Draw ( pDC , 200, 200 );