#include “atlimage.h” CImage img; HRESULT ret = img.Load(filename ); // filename 是要加载的文件名(包含路径) HBITMAP bitmap = img.Detach(); This function copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit the dimensions of the destination rectangle, if necessary. Windows stretches or compresses the bitmap according to the stretching mode set in the destination device context.BOOL StretchBlt( HDC hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, DWORD dwRop );
或者第三方开源图形库,如:CxImage
CImage img;
HRESULT ret = img.Load(filename ); // filename 是要加载的文件名(包含路径)
HBITMAP bitmap = img.Detach();
This function copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit the dimensions of the destination rectangle, if necessary. Windows stretches or compresses the bitmap according to the stretching mode set in the destination device context.BOOL StretchBlt(
HDC hdcDest,
int nXOriginDest,
int nYOriginDest,
int nWidthDest,
int nHeightDest,
HDC hdcSrc,
int nXOriginSrc,
int nYOriginSrc,
int nWidthSrc,
int nHeightSrc,
DWORD dwRop
);
void CxxxView::OnDraw(CDC* pDC)
{
::CoInitialize(NULL); // COM 初始化
HRESULT hr;
CFile file;
file.Open( "c:\\aa.jpg", 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 ); // 高
////////原大显示//////
CSize sz( nWidth, nHeight );
pDC->HIMETRICtoDP( &sz ); // 转换 MM_HIMETRIC 模式单位为 MM_TEXT 像素单位
pPicture->Render(pDC->m_hDC,0,0,sz.cx,sz.cy,
0,nHeight,nWidth,-nHeight,NULL);
////////按窗口尺寸显示////////
// CRect rect; GetClientRect(&rect);
// pPicture->Render(pDC->m_hDC,0,0,rect.Width(),rect.Height(),
// 0,nHeight,nWidth,-nHeight,NULL); if ( pPicture ) pPicture->Release();// 释放 IPicture 指针
if ( pStream ) pStream->Release(); // 释放 IStream 指针,同时释放了 hMem
::CoUninitialize();
给分!
http://www.codeproject.com/KB/graphics/cpicture.aspx
CxImage testImage("C:\\test.jpg",CXIMAGE_FORMAT_JPG);//加载图象
可以使用testImage.Draw()或者testImage.Stretch()缩放图象
比如:
testImage.Draw(pDC->GetSafeHdc(),CRect(0,0,(int)(ima->GetWidth()*zoom),(int)(ima->GetHeight()*zoom)),0,false);
//zoom表示缩放比率
CxImage testImage("C:\\test.jpg",CXIMAGE_FORMAT_JPG);//加载图象
可以使用testImage.Draw()或者testImage.Stretch()缩放图象
比如:
//OnDraw(CDC* pDC)函数中加入
testImage.Draw(pDC->GetSafeHdc(),CRect(0,0,(int)(testImage.GetWidth()*zoom),(int)(testImage.GetHeight()*zoom)),0,false);
//zoom表示缩放比率
CxImage testImage("C:\\test.jpg",CXIMAGE_FORMAT_JPG);//加载图象
可以使用testImage.Draw()或者testImage.Stretch()缩放图象
比如:
//OnDraw(CDC* pDC)加入
testImage.Draw(pDC->GetSafeHdc(),CRect(0,0,(int)(testImage.GetWidth()*zoom),(int)(testImage.GetHeight()*zoom)),0,false);
//zoom表示缩放比率
void CMnView::OnDraw(CDC* pDC)
{
CMSPrintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); Bitmap pngImg( L"R.PNG" ); //直接用Bitmap去png图片 jpg bmp gif代码上都是样的。
CRect rt;
GetClientRect( &rt ); //客户区 CBitmap bakeBitmap
CDC inMDC; inMDC.CreateCompatibleDC(NULL); //创建兼容DC
bakeBitmap.CreateCompatibleBitmap( pDC,rt.Width( ),rt.Height( ) ); //创建兼容位图
inMDC.SelectObject( &bakeBitmap ); //将兼容位图选入兼容DC,以后用inMDC绘制的所有东西其实都在这个bakeBitmap上了 inMDC.FillSolidRect( &rt,RGB( 255,255,255 ) ); //首先将bakeBitmap(客户区)填充成背景颜色,这里用的是白色。 Graphics graphics( inMDC.m_hDC ); //建立GDI+绘图句柄(取得是inMDC的句柄建立的,所以graphics的绘图跟inMDC的绘图其实是一样的。)
graphics.DrawImage( &pngImg, 300,300 ); //在bakeBitmap绘制原图大小的png图
graphics.DrawImage( &pngImg, 600,600, 200,50 ); //将原图起点变换到(600,600)固定宽度200 高度50再绘图 pDC->BitBlt( 0,0,rt.Width(),rt.Height(),&inMDC,0,0,SRCCOPY); //将bakeBitmap贴回客户区。 bakeBitmap.DeleteObject();
inMDC.DeleteDC();
}如果限制不多就是用GDI+的 很成熟的东西了 资源管理也很好 比较容易学
但是有一点 问题。
就是那个的用法问题。有没有人多给点小例子。要包含那些需要的错误处理等。谢谢了 。。