View内容保存为图片

我们在单文档应用程序中,经常需要将View中的内容保存为各种格式的图片文件,以便打印。乍一看,可能不知道从哪里下手,其实主要就是用到Bitmap的save方法,如:

HDC hmemDC = ::CreateCompatibleDC( hdc );  

HBITMAP hBmp = ::CreateCompatibleBitmap( hdc, destRect.Width(),destRect.Height() );

HANDLE hOld = ::SelectObject(hmemDC, hBmp);

Graphics graphic( hmemDC );

//下面进行各种文字、图形、图片的绘制
 …………………………………………….

Bitmap bitmap(hBmp, NULL );
CLSID clsID;

//保存不同格式的(.jpg,bmp,png)的图片需要不同的CLSID, imageFormat为用户期望保存的图片格式
if (_T("jpg") == imageFormat.MakeLower() )
{
      GetEncoderClsid(_T("image/jpeg"), &clsID);
}
else if ( _T("bmp") == imageFormat.MakeLower() )
{
      GetEncoderClsid(_T("image/bmp"), &clsID);
}
else if ( _T("png") == imageFormat.MakeLower() )
{
     GetEncoderClsid(_T("image/png"), &clsID);
}

//保存为图片,strFN为图片保存的路径和文件名
bitmap.Save( strFN, &clsID, NULL );
::SelectObject( hmemDC, hOld );
::DeleteObject( hBmp );
::DeleteDC( hmemDC );

 

       下面重要的就是获取不同图片格式的CLSID,看下面代码:

Int GetEncoderClsid(const TCHAR* format, CLSID* pClsid)
{ UINT num
= 0; UINT size= 0; ImageCodecInfo* pImageCodecInfo= NULL; GetImageEncodersSize(&num, &size); if(size== 0) { return -1; } pImageCodecInfo= (ImageCodecInfo*)(malloc(size)); if(pImageCodecInfo== NULL) { return -1; } GetImageEncoders(num, size, pImageCodecInfo); for(UINT j=0; j< num; ++j) { if(_tcscmp(pImageCodecInfo[j].MimeType, format)== 0) { *pClsid= pImageCodecInfo[j].Clsid; free(pImageCodecInfo); return j; } } free(pImageCodecInfo); return -1; }

 

如果是直接打印View中的图片,那么View的OnDraw函数给的pDC指的就是打印机的纸张的尺寸,我们无需做太多的处理,我们只需要将当期View的区域转换到纸张的大小即可,如:

CRect rc;
GetClientRect( &rc );

Long width =0,height = 0;
if( pDC->IsPrinting)
{
  int   xLogPixPerInch = pDC->GetDeviceCaps(LOGPIXELSX);
  int yLogPixPerInch = pDC->GetDeviceCaps(LOGPIXELSY);   //得到设备坐标和逻辑坐标的比例   long xExt = (long)rc.width() * xLogPixPerInch/96;   long yExt = (long)rc.height() * yLogPixPerInch/96;
  width
= xExt;   height = yExt;   //后面使用GDI+进行绘图   HBITMAP hBmp = ::CreateCompatibleBitmap( pDC->m_hDC, width, height );   HANDLE hOld = ::SelectObject(hmemDC, hBmp);   Graphics tmp_graff( hmemDC );   …………………………………..   //获取纸张的大小,然后拉伸拷贝   int iPageWidth = pDC->GetDeviceCaps(HORZRES);   int iPageHeight = pDC->GetDeviceCaps(VERTRES);   ::StretchBlt( pDC->m_hDC, 0, 0, iPageWidth, iPageHeight, hmemDC, 0, 0, width, height, SRCCOPY ); ::SelectObject( hmemDC, hOld );   ::DeleteObject( hBmp );   ::DeleteDC( hmemDC );
}

 

posted on 2013-03-07 19:50  RascallySnake  阅读(605)  评论(0编辑  收藏  举报