##
1.定义画图函数
HRESULT CIPCamDlg::draw(char *lpImageFile, HWND hWnd, int nScrWidth, int nScrHeight) {HDC hDC_Temp=::GetDC(hWnd);IPicture *pPic; IStream *pStm;BOOL bResult;HANDLE hFile=NULL; DWORD dwFileSize,dwByteRead;hFile=CreateFile(lpImageFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);//打开硬盘中的图形文件if (hFile!=INVALID_HANDLE_VALUE) { dwFileSize=GetFileSize(hFile,NULL);//获取文件字节数if (dwFileSize==0xFFFFFFFF) return E_FAIL; } else { return E_FAIL; }HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);//分配全局存储空间 LPVOID pvData = NULL;if (hGlobal == NULL) {AfxMessageBox("图形文件分配出错.");return E_FAIL; }if ((pvData = GlobalLock(hGlobal)) == NULL)//锁定分配内存块 {AfxMessageBox("内存块锁定出问题");return E_FAIL;}ReadFile(hFile,pvData,dwFileSize,&dwByteRead,NULL);//把文件读入内存缓冲区 GlobalUnlock(hGlobal);if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStm) != S_OK){AfxMessageBox("流初始化失败");return E_FAIL; }bResult=OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID*)&pPic);//装入图形文件 if(FAILED(bResult)) {AfxMessageBox("图形文件装载出错.");return E_FAIL; }OLE_XSIZE_HIMETRIC hmWidth;//图片的真实宽度 OLE_YSIZE_HIMETRIC hmHeight;//图片的真实高度 pPic->get_Width(&hmWidth); pPic->get_Height(&hmHeight);//char str[20]={'\0'}; bResult=pPic->Render(hDC_Temp,0,0,nScrWidth,nScrHeight,0,hmHeight,hmWidth,-hmHeight,NULL);//将图形输出到屏幕上 CloseHandle(hFile);//关闭打开的文件 pPic->Release(); GlobalFree(hGlobal);// Free memory. if (SUCCEEDED(bResult)) { return S_OK; } else { AfxMessageBox("图形文件装载出错.");return E_FAIL; } }
2.调用
HWND m_hWnd; GetDlgItem(IDC_STATIC_VIEW,&m_hWnd); //IDC_STATIC_VIEW是picture控件(不用做任何修改) draw("D://a.jpg",m_hWnd,宽度,高度);