Load and Show an Image on MFC. In this way, you can load an *.bmp Image to show on a component on you MFC form
// Load an Bitmap Image
HBITMAP ImageShower::LoadBitmapFromFile(CString path)
{
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,
path,
IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
return hBmp;
}
// Show an HBITMAP on an DC
void ImageShower::ShowBitmapOnDC(HBITMAP hBmp, CWnd* showPlace)
{
CBitmap bmp;
bmp.Attach(hBmp);
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
// Get rectangle of show place
CRect rec;
showPlace->GetWindowRect(&rec);
// Initialize DC
CClientDC dc(showPlace);
CDC bmDC;
bmDC.CreateCompatibleDC(&dc);
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
BITMAP bi;
bmp.GetBitmap(&bi);
// Scale Image and show full on DC
dc.StretchBlt(0,0,
rec.Width(), rec.Height(),
&bmDC,0,0,bmpInfo.bmWidth, bmpInfo.bmHeight,SRCCOPY);
bmDC.SelectObject(pOldbmp);
}
// How to use these functions ?
Example:
// Path of image
CString path("C:\\cLena.bmp");
// Load bitmap
HBITMAP hBmp = LoadBitmapFromFile(path);
// Get Handle of Item
CWnd* mP = GetDlgItem(IDC_STATIC);
// Show bitmap on Item
ShowBitmapOnDC(hBmp,mP);