下面的代码把路径和控件ID替换一下就能用了IPicture*m_picture;OLE_XSIZE_HIMETRICm_width;OLE_YSIZE_HIMETRICm_height;CStringm_filenamecFilePath;//读取的文件名CFilem_filem_filename,CFile::modeRead;//获取文件长度DWORDm_filelen=m_file.GetLength;//在堆上分配空间HGLOBALm_hglobal=GlobalAllocGMEM_MOVEABLE,m_filelen;LPVOIDpvdata=NULL;//锁定堆空间,获取指向堆空间的指针pvdata=GlobalLockm_hglobal;//将文件数据读区到堆中m_file.Readpvdata,m_filelen;IStream*m_stream;GlobalUnlockm_hglobal;//在堆中创建流对象CreateStreamOnHGlobalm_hglobal,TRUE,&m_stream;//利用流加载图像OleLoadPicturem_stream,m_filelen,TRUE,IID_IPicture,LPVOID*&m_picture;m_picture->get_Width&m_width;m_picture->get_Height&m_height;CDC*dc=GetDC;//在控件范围显示图片CRectrect;GetDlgItemIDC_STATIC_PREVIEW->GetWindowRect▭ScreenToClient▭m_picture->Render*dc,rect.left,rect.top,rect.Width,rect.Height,0,m_height,m_width,-m_height,NULL;m_picture->Release。
符翠红2020-01-16 23:20:07
放在对话框的OnPaint函数即可。CDC* pDC; pDC=GetDC; ShowPicturepDC,"c:\\12.jpg",0,0,200,100; #define HIMETRIC_INCH 2540 LPPICTURE gpPicture; void CCDMADlg::ShowPictureCDC *pDC, CString m_strBRoute, int x, int y, int width, int height { HANDLE hFile =CreateFilem_strBRoute, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL; _ASSERTEINVALID_HANDLE_VALUE != hFile; // 取得文件大小 DWORD dwFileSize = GetFileSizehFile, NULL; _ASSERTE-1 != dwFileSize; LPVOID pvData = NULL; //根据文件大小分配内存 HGLOBAL hGlobal = GlobalAllocGMEM_MOVEABLE, dwFileSize; _ASSERTENULL != hGlobal; pvData = GlobalLockhGlobal; _ASSERTENULL != pvData; DWORD dwBytesRead = 0; //读取文件并存入全局内存 BOOL bRead = ReadFilehFile, pvData, dwFileSize, &dwBytesRead, NULL; _ASSERTEFALSE != bRead; GlobalUnlockhGlobal; CloseHandlehFile; LPSTREAM pstm = NULL; // 通过全局内存创建 IStream* 的指针 HRESULT hr = CreateStreamOnHGlobalhGlobal, TRUE, &pstm; _ASSERTESUCCEEDEDhr && pstm; //通过图形文件创建IPicture 对象 if gpPicture gpPicture->Release; hr = OleLoadPicturepstm, dwFileSize, FALSE, IID_IPicture, LPVOID *&gpPicture; _ASSERTESUCCEEDEDhr && gpPicture; pstm->Release; HDC hdc; hdc=pDC->GetSafeHdc; if gpPicture { // 取得图片的宽和高 long hmWidth; long hmHeight; gpPicture->get_Width&hmWidth; gpPicture->get_Height&hmHeight; //宽高转换为象素 int nWidth = MulDivhmWidth, GetDeviceCapshdc, LOGPIXELSX, HIMETRIC_INCH; int nHeight = MulDivhmHeight, GetDeviceCapshdc, LOGPIXELSY, HIMETRIC_INCH; RECT rc; GetClientRect&rc;/*取得客户区*/ gpPicture->Renderhdc, x,y, intheight*hmWidth/hmHeight,height, 0, hmHeight, hmWidth, -hmHeight, &rc; /*显示图片*/ } }http://www.cppblog.com/wanghaiguang/archive/2019/09/10/190136.html。
齐晓杰2020-01-16 23:02:14