以下代码使用IPicture接口显示图片,可直接放于OnDraw函数中。
IPictrue接口支持BMP、DIB、EMF、GIF、ICO、JPG、WMF格式图片的显示,但只能保存BMP和ICO格式的图片。
//
pDoc为文档对象指针
// pDC为设备描述表指针
::CoInitialize(NULL); // COM 初始化
HRESULT hr;
CFile file;
file.Open(pDoc -> GetPathName(), CFile::modeRead | CFile::shareDenyNone ); // 读入文件内容
DWORD dwSize = file.GetLength();
HGLOBAL hMem = ::GlobalAlloc( GMEM_MOVEABLE, dwSize );
LPVOID lpBuf = ::GlobalLock( hMem );
file.ReadHuge( lpBuf, dwSize );
file.Close();
::GlobalUnlock( hMem );
IStream * pStream = NULL;
IPicture * pPicture = NULL;
// 由 HGLOBAL 得到 IStream,参数 TRUE 表示释放 IStream 的同时,释放内存
hr = ::CreateStreamOnHGlobal( hMem, TRUE, & pStream );
ASSERT ( SUCCEEDED(hr) );
hr = ::OleLoadPicture( pStream, dwSize, TRUE, IID_IPicture, ( LPVOID * ) & pPicture );
ASSERT(hr == S_OK);
long nWidth,nHeight; // 宽高,MM_HIMETRIC 模式,单位是0.01毫米
pPicture -> get_Width( & nWidth ); // 宽
pPicture -> get_Height( & nHeight ); // 高
CRect rect;
GetClientRect( & rect);
CSize sz( nWidth, nHeight );
pDC -> HIMETRICtoDP( & sz ); // 转换 MM_HIMETRIC 模式单位为 MM_TEXT 像素单位
long x, y, cx, cy;
// 原始大小
/*
cx = sz.cx;
cy = sz.cy;
x = rect.Width() / 2 - cx / 2;
y = rect.Height() / 2 - cy / 2;
*/
// 自动适应窗口
double fRatePic, fRateWnd;
fRatePic = ( double )sz.cx / ( double )sz.cy;
fRateWnd = ( double )rect.Width() / ( double )rect.Height();
if (fRatePic > fRateWnd)
{
cx = rect.Width();
cy = ( long )(rect.Width() / fRatePic);
}
else
{
cx = ( long )(rect.Height() * fRatePic);
cy = rect.Height();
}
if (cx == rect.Width())
{
x = 0 ;
y = rect.Height() / 2 - cy / 2 ;
}
if (cy == rect.Height())
{
x = rect.Width() / 2 - cx / 2 ;
y = 0 ;
}
pPicture -> Render(pDC -> m_hDC, x, y, cx, cy,
0 , nHeight, nWidth, - nHeight, NULL);
if ( pPicture ) pPicture -> Release(); // 释放 IPicture 指针
if ( pStream ) pStream -> Release(); // 释放 IStream 指针,同时释放了 hMem
::CoUninitialize();
// pDC为设备描述表指针
::CoInitialize(NULL); // COM 初始化
HRESULT hr;
CFile file;
file.Open(pDoc -> GetPathName(), CFile::modeRead | CFile::shareDenyNone ); // 读入文件内容
DWORD dwSize = file.GetLength();
HGLOBAL hMem = ::GlobalAlloc( GMEM_MOVEABLE, dwSize );
LPVOID lpBuf = ::GlobalLock( hMem );
file.ReadHuge( lpBuf, dwSize );
file.Close();
::GlobalUnlock( hMem );
IStream * pStream = NULL;
IPicture * pPicture = NULL;
// 由 HGLOBAL 得到 IStream,参数 TRUE 表示释放 IStream 的同时,释放内存
hr = ::CreateStreamOnHGlobal( hMem, TRUE, & pStream );
ASSERT ( SUCCEEDED(hr) );
hr = ::OleLoadPicture( pStream, dwSize, TRUE, IID_IPicture, ( LPVOID * ) & pPicture );
ASSERT(hr == S_OK);
long nWidth,nHeight; // 宽高,MM_HIMETRIC 模式,单位是0.01毫米
pPicture -> get_Width( & nWidth ); // 宽
pPicture -> get_Height( & nHeight ); // 高
CRect rect;
GetClientRect( & rect);
CSize sz( nWidth, nHeight );
pDC -> HIMETRICtoDP( & sz ); // 转换 MM_HIMETRIC 模式单位为 MM_TEXT 像素单位
long x, y, cx, cy;
// 原始大小
/*
cx = sz.cx;
cy = sz.cy;
x = rect.Width() / 2 - cx / 2;
y = rect.Height() / 2 - cy / 2;
*/
// 自动适应窗口
double fRatePic, fRateWnd;
fRatePic = ( double )sz.cx / ( double )sz.cy;
fRateWnd = ( double )rect.Width() / ( double )rect.Height();
if (fRatePic > fRateWnd)
{
cx = rect.Width();
cy = ( long )(rect.Width() / fRatePic);
}
else
{
cx = ( long )(rect.Height() * fRatePic);
cy = rect.Height();
}
if (cx == rect.Width())
{
x = 0 ;
y = rect.Height() / 2 - cy / 2 ;
}
if (cy == rect.Height())
{
x = rect.Width() / 2 - cx / 2 ;
y = 0 ;
}
pPicture -> Render(pDC -> m_hDC, x, y, cx, cy,
0 , nHeight, nWidth, - nHeight, NULL);
if ( pPicture ) pPicture -> Release(); // 释放 IPicture 指针
if ( pStream ) pStream -> Release(); // 释放 IStream 指针,同时释放了 hMem
::CoUninitialize();