使用IPicture::Render 繪制圖片的方法說(shuō)明:
HRESULT Render( HDC hdc, //Handle of device context on which to render the image //繪圖設(shè)備 long x, //Horizontal position of image in hdc //繪圖設(shè)備上的X起始坐標(biāo) long y, //Vertical position of image in hdc //繪圖設(shè)備上的Y起始坐標(biāo) long cx, //Horizontal dimension of destination rectangle //繪圖設(shè)備上的水平像素單位數(shù)(寬) long cy, //Vertical dimension of destination rectangle //繪圖設(shè)備上的垂直像素單位數(shù)(高) OLE_XPOS_HIMETRIC xSrc, //Horizontal offset in source picture //原圖的X起始坐標(biāo) OLE_YPOS_HIMETRIC ySrc, //Vertical offset in source picture //原圖的Y起始坐標(biāo) OLE_XSIZE_HIMETRIC cxSrc, //Amount to copy horizontally in source picture //總計(jì)拷貝的水平像素單位數(shù)(寬) OLE_YSIZE_HIMETRIC cySrc, //Amount to copy vertically in source picture //總計(jì)拷貝的垂直像素單位數(shù)(高) LPCRECT prcWBounds //Pointer to position of destination for a metafile hdc //圖源文件指針 ); 范例: HRESULT hr=m_lppi->Render(pDC->m_hDC,0,0,100,100,0,0,11774,20320,&rc); 使用CreateFile取得文件句柄的方法說(shuō)明 HANDLE WINAPI CreateFile( LPCTSTR lpFileName, //The name of the object to be created or opened. //打開(kāi)或者新建的文件名 DWORD dwDesiredAccess, // The access to the object, which can be read, write, or both. // 文件訪問(wèn)權(quán)限 常用的是 GENERIC_EXECUTE / GENERIC_READ /GENERIC_WRITE DWORD dwShareMode, // The sharing mode of an object, which can be read, write, both, or none // 文件的共享模式,常用的是 FILE_SHARE_DELETE / FILE_SHARE_READ /FILE_SHARE_WRITE ,0表示不共享 LPSECURITY_ATTRIBUTES lpSecurityAttributes, // A pointer to a SECURITY_ATTRIBUTES structure that determines whether or not the returned handle can be inherited by child processes. // 詳細(xì)內(nèi)容,參見(jiàn) msdn 的相關(guān)描述,我就不翻譯了 DWORD dwCreationDisposition, // An action to take on files that exist and do not exist. // 詳細(xì)內(nèi)容,參見(jiàn) msdn 的相關(guān)描述,我就不翻譯了 DWORD dwFlagsAndAttributes, // The file attributes and flags. // 詳細(xì)內(nèi)容,參見(jiàn) msdn 的相關(guān)描述,我就不翻譯了 HANDLE hTemplateFile // A handle to a template file with the GENERIC_READ access right. The template file supplies file attributes and extended attributes for the file that is being created. This parameter can be NULL. // 詳細(xì)內(nèi)容,參見(jiàn) msdn 的相關(guān)描述,我就不翻譯了 ); 范例: HANDLE hFile=CreateFile(_T("\\aaa.jpg"),GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL); 使用IPersistStream::Load獲取LPPICTURE對(duì)象的方法: STDAPI OleLoadPicture( IStream * pStream, //Pointer to the stream that contains picture's data LONG lSize, //Number of bytes read from the stream BOOL fRunmode, //The opposite of the initial value of the picture's // property REFIID riid, //Reference to the identifier of the interface // describing the type of interface pointer to return VOID ppvObj //Address of output variable that receives interface // pointer requested in riid ); 其他方法: //按文件大小分配內(nèi)存 LPVOID pvData; HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize); //鎖定內(nèi)存 pvData=GlobalLock(hGlobal); //讀取文件到內(nèi)存 DWORD dwFileRead=0; BOOL bRead=ReadFile(hFile,pvData,dwFileSize,&dwFileRead,NULL); //從已分配內(nèi)存生成IStream流 HRESULT hr=CreateStreamOnHGlobal(hGlobal,TRUE,&pstm); hr=OleLoadPicture(pstm,dwFileSize,FALSE,IID_IPicture,(LPVOID*)&(*lppi)); pstm->Release(); 一個(gè)相對(duì)完整的步驟 //加載圖片 BOOL CPicTestDlg::LoadMyJpegFile(CString fname,LPPICTURE *lppi) { HANDLE hFile=CreateFile(fname,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL); if(hFile==INVALID_HANDLE_VALUE) { CString str; str.Format(_T("%s無(wú)法被打開(kāi)"),fname); MessageBox(str); return FALSE; } //取得文件大小 DWORD dwFileSize=GetFileSize(hFile,NULL); if((DWORD)-1==dwFileSize) { CloseHandle(hFile); MessageBox(_T("圖像文件是空的")); return FALSE; } //讀取圖像文件 LPVOID pvData; //按文件大小分配內(nèi)存 HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize); if(NULL==hGlobal) { CloseHandle(hFile); MessageBox(_T("內(nèi)存不足,無(wú)法分配足夠內(nèi)存")); return FALSE; } pvData=GlobalLock(hGlobal); if(NULL==pvData) { GlobalUnlock(hGlobal); CloseHandle(hFile); MessageBox(_T("無(wú)法鎖定內(nèi)存")); return FALSE; } DWORD dwFileRead=0; BOOL bRead=ReadFile(hFile,pvData,dwFileSize,&dwFileRead,NULL); GlobalUnlock(hGlobal); CloseHandle(hFile); if(FALSE==bRead) { MessageBox(_T("讀文件出錯(cuò)")); return FALSE; } LPSTREAM pstm=NULL; //從已分配內(nèi)存生成IStream流 HRESULT hr=CreateStreamOnHGlobal(hGlobal,TRUE,&pstm); if(!SUCCEEDED(hr)) { MessageBox(_T("生成流操作失敗")); if(pstm!=NULL) pstm->Release(); return FALSE; }else if(pstm==NULL){ MessageBox(_T("生成流操作失敗")); return FALSE; } if(!*lppi) (*lppi)->Release(); hr=OleLoadPicture(pstm,dwFileSize,FALSE,IID_IPicture,(LPVOID*)&(*lppi)); pstm->Release(); if(!SUCCEEDED(hr)) { MessageBox(_T("加載操作失敗")); return FALSE; }else if(*lppi==NULL){ MessageBox(_T("加載操作失敗")); return FALSE; } return TRUE; } //繪制圖片 TCHAR strPath[MAX_PATH]; memset(strPath,0,MAX_PATH); //得到當(dāng)前路徑 GetCurrentDirectory(MAX_PATH,strPath); //定義圖片路徑 wcscat_s(strPath,MAX_PATH,_T("\\145.bmp")); //加載圖片到 m_lppi m_bHadLoad=LoadMyJpegFile(strPath,&m_lppi); //取得繪圖設(shè)備 CDC *pDC=GetDC(); //定義繪圖矩形區(qū)域 CRect rc; //得到圖片長(zhǎng)寬 long hmWidth=0; long hmHeight=0; m_lppi->get_Height(&hmHeight); m_lppi->get_Width(&hmWidth); //定義區(qū)域與設(shè)備關(guān)聯(lián) GetClientRect(&rc); int nWidth,nHeight; //得到設(shè)備的長(zhǎng)寬 nWidth=rc.Width(); nHeight=rc.Height(); //繪制圖片到設(shè)備區(qū)域 HRESULT hr=m_lppi->Render(pDC->m_hDC,nWidth,0,-nWidth,nHeight,hmWidth,hmHeight,-hmWidth,-hmHeight,&rc); 使用以上內(nèi)容可以在mfc的窗體中的任何地方繪制圖片,重繪的時(shí)候,需要在方法OnPaint()中加以定義,另外可以在OnInitDialog()中提前加載圖片到內(nèi)存中. |
|