使用IPicture::Render 繪制圖片的方法說(shuō)明:
HRESULT Render(
HDC hdc, //Handle of device context on which to render the image
//繪圖設備
long x, //Horizontal position of image in hdc
//繪圖設備上的X起始坐標
long y, //Vertical position of image in hdc
//繪圖設備上的Y起始坐標
long cx, //Horizontal dimension of destination rectangle
//繪圖設備上的水平像素單位數(寬)
long cy, //Vertical dimension of destination rectangle
//繪圖設備上的垂直像素單位數(高)
OLE_XPOS_HIMETRIC xSrc,
//Horizontal offset in source picture
//原圖的X起始坐標
OLE_YPOS_HIMETRIC ySrc,
//Vertical offset in source picture
//原圖的Y起始坐標
OLE_XSIZE_HIMETRIC cxSrc,
//Amount to copy horizontally in source picture
//總計拷貝的水平像素單位數(寬)
OLE_YSIZE_HIMETRIC cySrc,
//Amount to copy vertically in source picture
//總計拷貝的垂直像素單位數(高)
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.
// 文件訪(fǎng)問(wè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.
// 詳細內容,參見(jiàn) msdn 的相關(guān)描述,我就不翻譯了
DWORD dwCreationDisposition,
// An action to take on files that exist and do not exist.
// 詳細內容,參見(jiàn) msdn 的相關(guān)描述,我就不翻譯了
DWORD dwFlagsAndAttributes,
// The file attributes and flags.
// 詳細內容,參見(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.
// 詳細內容,參見(jiàn) msdn 的相關(guān)描述,我就不翻譯了
);
范例:
HANDLE hFile=CreateFile(_T("\\aaa.jpg"),GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
使用IPersistStream::Load獲取LPPICTURE對象的方法:
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
);
其他方法:
//按文件大小分配內存
LPVOID pvData;
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
//鎖定內存
pvData=GlobalLock(hGlobal);
//讀取文件到內存
DWORD dwFileRead=0;
BOOL bRead=ReadFile(hFile,pvData,dwFileSize,&dwFileRead,NULL);
//從已分配內存生成IStream流
HRESULT hr=CreateStreamOnHGlobal(hGlobal,TRUE,&pstm);
hr=OleLoadPicture(pstm,dwFileSize,FALSE,IID_IPicture,(LPVOID*)&(*lppi));
pstm->Release();
一個(gè)相對完整的步驟
//加載圖片
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;
//按文件大小分配內存
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
if(NULL==hGlobal)
{
CloseHandle(hFile);
MessageBox(_T("內存不足,無(wú)法分配足夠內存"));
return FALSE;
}
pvData=GlobalLock(hGlobal);
if(NULL==pvData)
{
GlobalUnlock(hGlobal);
CloseHandle(hFile);
MessageBox(_T("無(wú)法鎖定內存"));
return FALSE;
}
DWORD dwFileRead=0;
BOOL bRead=ReadFile(hFile,pvData,dwFileSize,&dwFileRead,NULL);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
if(FALSE==bRead)
{
MessageBox(_T("讀文件出錯"));
return FALSE;
}
LPSTREAM pstm=NULL;
//從已分配內存生成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);
//得到當前路徑
GetCurrentDirectory(MAX_PATH,strPath);
//定義圖片路徑
wcscat_s(strPath,MAX_PATH,_T("\\145.bmp"));
//加載圖片到 m_lppi
m_bHadLoad=LoadMyJpegFile(strPath,&m_lppi);
//取得繪圖設備
CDC *pDC=GetDC();
//定義繪圖矩形區域
CRect rc;
//得到圖片長(cháng)寬
long hmWidth=0;
long hmHeight=0;
m_lppi->get_Height(&hmHeight);
m_lppi->get_Width(&hmWidth);
//定義區域與設備關(guān)聯(lián)
GetClientRect(&rc);
int nWidth,nHeight;
//得到設備的長(cháng)寬
nWidth=rc.Width();
nHeight=rc.Height();
//繪制圖片到設備區域
HRESULT hr=m_lppi->Render(pDC->m_hDC,nWidth,0,-nWidth,nHeight,hmWidth,hmHeight,-hmWidth,-hmHeight,&rc);
使用以上內容可以在mfc的窗體中的任何地方繪制圖片,重繪的時(shí)候,需要在方法OnPaint()中加以定義,另外可以在OnInitDialog()中提前加載圖片到內存中.