将JPG图片绘制在对话框背景上:(这段代码绝对可以执行)
将JPG图片绘制在对话框背景上:
显示JPG图片的函数
void CMyDlg::ShowPicture(CDC *pDC, CString m_strBRoute, int x, int y, int width, int height)
{
HANDLE hFile = CreateFile(m_strBRoute, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
_ASSERTE(INVALID_HANDLE_VALUE != hFile);
// 取得文件大小
DWORD dwFileSize = GetFileSize(hFile, NULL);
_ASSERTE(-1 != dwFileSize);
LPVOID pvData = NULL;
//根据文件大小分配内存
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
_ASSERTE(NULL != hGlobal);
pvData = GlobalLock(hGlobal);
_ASSERTE(NULL != pvData);
DWORD dwBytesRead = 0;
//读取文件并存入全局内存
BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
_ASSERTE(FALSE != bRead);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
LPSTREAM pstm = NULL;
// 通过全局内存创建 IStream* 的指针
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
_ASSERTE(SUCCEEDED(hr) && pstm);
//通过图形文件创建IPicture 对象
if (gpPicture)
gpPicture->Release();
hr = OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
_ASSERTE(SUCCEEDED(hr) && gpPicture);
pstm->Release();
HDC hdc;
hdc=pDC->GetSafeHdc();
if (gpPicture)
{
// 取得图片的宽和高
long hmWidth;
long hmHeight;
gpPicture->get_Width(&hmWidth);
gpPicture->get_Height(&hmHeight);
//宽高转换为象素
int nWidth = MulDiv(hmWidth, GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH);
int nHeight = MulDiv(hmHeight, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH);
RECT rc;
GetClientRect(&rc);/*取得客户区*/
gpPicture->Render(hdc, x,y, (int)height*hmWidth/hmHeight,height, 0, hmHeight, hmWidth, -hmHeight, &rc);
/*显示图片*/
}
GlobalFree(hGlobal);
}
//在OnEraseBkgnd中
CRect rc;
GetClientRect(&rc);
ShowPicture(pDC,strJpgPath,rc.left,rc.top,rc.Width(), rc.Height());
<linker : http://www.cnblogs.com/8586/archive/2008/11/17/1335194.html>