Qt 导出图像(代码保存)

bool CSSCEViewerMainFrame::SaveCurrentPageToPicture( int eType, QSize szDest )
{
 
  
    QImage image( szDest, QImage::Format_RGB32 );
 
  
    ExportCurrentPageToBMP( image, szDest );
 
  
    CSSEMainViewPro* pView = m_pMultiViewManager->GetFirstValidView( true );
 
  
    QString strFileName;
 
  
    strFileName = QString("%1_[%2]_%3").arg( pView == NULL ? "" : pView->GetDcmTagValueList( "0010,0020_0010,0010_0010,0040_0010,1010_0008,0060_0008,0020", "_" ) )
            .arg( CSSECfgSessionInfo::GetUserName() ).arg( CSpiderGUID::HashPJW() );
 
  
    QString str = m_pCfgFileDirectory->GetDirectory( "DIRECTORY_GLOBAL_EXPORT", true );
	str.replace("/","\\");
 
  
    QString strDir, strExtFilname;
 
  
    switch( eType )
    {
        case IDN_METRO_EXPORT_IMAGEFILE_BTN_JPEG:
        {
            strFileName = str + "JPEG" + "\\" + strFileName + "." + "jpeg" ;
            strDir = str + "JPEG";
            strExtFilname = "JPEG";
        }
            break;
 
  
        case IDN_METRO_EXPORT_IMAGEFILE_BTN_BMP:
        {
            strFileName = str + "BMP" + "\\" + strFileName + "." + "bmp" ;
            strDir = str + "BMP";
            strExtFilname = "BMP";
        }
            break;
 
  
        case IDN_METRO_EXPORT_IMAGEFILE_BTN_PNG:
        {
            strFileName = str + "PNG" + "\\" + strFileName + "." + "png" ;
            strDir = str + "PNG";
            strExtFilname = "PNG";
        }
            break;
 
  
        case IDN_METRO_EXPORT_IMAGEFILE_BTN_TIFF:
        {
            strFileName = str + "TIFF" + "\\" + strFileName + "." + "tiff" ;
            strDir = str + "TIFF";
            strExtFilname = "TIFF";
        }
            break;
        case IDN_METRO_EXPORT_IMAGEFILE_BTN_PGM:
        {
            strFileName = str + "PGM" + "\\" + strFileName + "." + "pgm" ;
            strDir = str + "PGM";
            strExtFilname = "PGM";
        }
            break;
        case IDN_METRO_EXPORT_IMAGEFILE_BTN_PPM:
        {
            strFileName = str + "PPM" + "\\" + strFileName + "." + "ppm" ;
            strDir = str + "PPM";
            strExtFilname = "PPM";
        }
            break;
    }
 
  
    QDir *imageDir = new QDir( strDir );
    bool exist = imageDir->exists( strDir );
 
  
    if( !exist )
    {
      imageDir->mkpath( strDir );
    }
 
  
    delete imageDir;
    imageDir = NULL;
 
  
    bool  bResult = false;
 
  
    bResult = image.save( strFileName );
 
  
    if( bResult )
    {
        m_hitText  = new  CSSCEViewerLabel( this , strFileName );
    }
    else
    {
        m_hitText  = new  CSSCEViewerLabel( this , "图像导出失败!" );
    }
 
  
    return bResult;
}
 
  
 
  
 ExportCurrentPageToBMP( image, szDest )  调用   
ExportCurrentPage  函数
 
  
 
  
bool CSSCEBaseExMultiViewManager::ExportCurrentPage( QSize szImage, QImage* pImage )
{
    float fScaleX = float(szImage.width()) / float(m_rcRealClient.width());
    float fScaleY = float(szImage.height()) / float(m_rcRealClient.height());
 
  
    float fScale = fScaleX > fScaleY ? fScaleX : fScaleY;
 
  
    QVector<QRect> vtrLayout;
    int	nFirstView;
    GetCurrentPageLayout( QRect( QPoint(0,0), szImage ), vtrLayout, nFirstView );  // 获得不同分格的View窗口布局,许多个小窗格
 
  
    QPainter pPainter(pImage);
    pPainter.setRenderHint(QPainter::Antialiasing, false);
    pPainter.setRenderHint(QPainter::HighQualityAntialiasing, false);
 
  
    for( int i=0; i< vtrLayout.size(); i++ )           //把View中每个小的窗格画到不同的QImage上,再把这些小的QImage画到同一个大的QImage上
    {
        QRect rcNew = vtrLayout[i];
 
  
        QImage signalImage( rcNew.size(), QImage::Format_RGB32 );
        QImage * psignalImage = &signalImage;
 
  
        CSSEMainView * pView = GetView(nFirstView+i);
 
  
        pView->DrawCoreToQImage( psignalImage, QRect( QPoint(0,0), rcNew.size() ) , fScale );
 
  
        pPainter.drawImage( QRectF(rcNew), *psignalImage, QRectF(psignalImage->rect()) );
 
  
    }
 
  
    return true;
}

 
  
DrawCoreToQImage 调用 DrawCoreToUnknow  函数

void CSSEMainView::DrawCoreToUnknow( QPainter* pPainter, QImage* &pImage, bool bToPainter, QRect rcNewSize, float fScale )
{    
	if ( m_pSSEImage->IsValid() )
	{//BEGIN
		QRect rcOrgView = m_rcClient;
		float fOrgZoomScale = m_pSSEImage->GetZoomScale();
 
  
		// 图像先放大
		if( fScale != 1.0f )
		{
			m_rcClient = rcNewSize;
 
  
			m_pSSEImage->OnSize( m_rcClient );
			m_pSSEImage->ZoomImageEx( fScale );
		}
 
  
		if( bToPainter )
		{
			QDateTime dtStart = QDateTime::currentDateTime();
 
  
			QPainter* pLocalPainter = NULL;
 
  
			if( pPainter == NULL )
			{
				pLocalPainter = new QPainter(this);
				pPainter = pLocalPainter;
			}
 
  
			pPainter->setRenderHint(QPainter::Antialiasing, false);
			pPainter->setRenderHint(QPainter::HighQualityAntialiasing, false);
 
  
#if defined(WIN32)
			pPainter->setRenderHint(QPainter::SmoothPixmapTransform, !CMouseUtility::IsAnyButtonDown() );
            pPainter->drawImage( m_pSSEImage->GetDisplayImageViewRect().toRect(), *(m_pSSEImage->GetMappedImage()), m_pSSEImage->GetDisplayImageVisibleRect() );
//			pPainter->drawPixmap( m_pSSEImage->GetDisplayImageViewRect(), *(m_pSSEImage->GetDisplayImage()), m_pSSEImage->GetDisplayImageVisibleRect() );
#else
			pPainter->setRenderHint(QPainter::SmoothPixmapTransform, !CMouseUtility::IsAnyButtonDown() );
            pPainter->drawImage( m_pSSEImage->GetDisplayImageViewRect().toRect(), *(m_pSSEImage->GetMappedImage()), m_pSSEImage->GetDisplayImageVisibleRect() );
#endif
 
  
            //if( !CMouseUtility::IsAnyButtonWorking() )zqj
			{
				DrawElements( pPainter );
			}
 
  
			{
//				QDateTime dtCurrent = QDateTime::currentDateTime();
 
  
//				QPen pen( QColor( 255, 100, 0 ) );
//				pPainter->setPen(pen);
//				pLocalPainter->drawText( QPoint( 10, 20 ), QString::number( dtStart.msecsTo( dtCurrent  ) ) );
 
  
//				dtStart = dtCurrent;
//				qDebug() << "Draw View: " <<  m_nViewID;
			}
 
  
			if( pLocalPainter != NULL )
			{
				delete pLocalPainter;
			}
		}
		else
		{
            if( pImage == NULL )
            {
              pImage = new QImage( m_rcClient.size(), QImage::Format_RGB32 );
            }
 
  
            QPainter localPainter(pImage);
 
  
            localPainter.setRenderHint(QPainter::Antialiasing, true);
            localPainter.setRenderHint(QPainter::HighQualityAntialiasing, true);
            localPainter.setRenderHint(QPainter::SmoothPixmapTransform, true );
            localPainter.drawImage(m_pSSEImage->GetDisplayImageViewRect(), *(m_pSSEImage->GetMappedImage()), m_pSSEImage->GetDisplayImageVisibleRect() );
 
  
            DrawElements( &localPainter );
 
  
 
  
		}
 
  
		// 图像缩小
		if( fScale != 1.0f )
		{
			m_rcClient = rcOrgView;
			m_pSSEImage->OnSize( m_rcClient );
			m_pSSEImage->ZoomImage( fOrgZoomScale );
		}
 
  
	}//END
	else
	{
		QRect rcOrgView = m_rcClient;
 
  
		// 图像先放大
		if( fScale != 1.0f )
		{
			m_rcClient = rcNewSize;
		}
 
  
		if( bToPainter )
		{
			QPainter* pLocalPainter = NULL;
 
  
			if( pPainter == NULL )
			{
				pLocalPainter = new QPainter(this);
				pPainter = pLocalPainter;
			}
 
  
            DrawElements( pPainter );
 
  
			// 绘制外边框
            {
                QPen pen( QColor( 109, 140, 231, 255 ) );
				pPainter->setPen( pen );
 
  
                //liyanbo 画笔和预留一个像素
//                pPainter->drawRect( rect().adjusted( 0, 0, -1, -1 )  );
			}
 
  
			delete pLocalPainter;
		}
		else
		{
            if( pImage == NULL )
            {
                pImage = new QImage( m_rcClient.size(), QImage::Format_RGB32 );
            }
 
  
			QPainter localPainter(pImage);
 
  
			DrawElements( &localPainter );
		}
 
  
 
  
		// 图像缩小
		if( fScale != 1.0f )
		{
			m_rcClient = rcOrgView;
		}
	}
}

你可能感兴趣的:(QT)