@@ -2887,16 +2887,32 @@ bool QgsComposition::print( QPrinter &printer, const bool evaluateDDPageSize )
28872887 return true ;
28882888}
28892889
2890- QImage QgsComposition::printPageAsRaster ( int page )
2890+ QImage QgsComposition::printPageAsRaster ( int page, const QSize& imageSize, int dpi )
28912891{
2892- // print out via QImage, code copied from on_mActionExportAsImage_activated
2893- int width = ( int )( printResolution () * paperWidth () / 25.4 );
2894- int height = ( int )( printResolution () * paperHeight () / 25.4 );
2892+ int resolution = mPrintResolution ;
2893+ if ( imageSize.isValid () )
2894+ {
2895+ // output size in pixels specified, calculate resolution using average of
2896+ // derived x/y dpi
2897+ resolution = ( imageSize.width () / mPageWidth
2898+ + imageSize.height () / mPageHeight ) / 2.0 * 25.4 ;
2899+ }
2900+ else if ( dpi > 0 )
2901+ {
2902+ // dpi overridden by function parameters
2903+ resolution = dpi;
2904+ }
2905+
2906+ int width = imageSize.isValid () ? imageSize.width ()
2907+ : ( int )( resolution * mPageWidth / 25.4 );
2908+ int height = imageSize.isValid () ? imageSize.height ()
2909+ : ( int )( resolution * mPageHeight / 25.4 );
2910+
28952911 QImage image ( QSize ( width, height ), QImage::Format_ARGB32 );
28962912 if ( !image.isNull () )
28972913 {
2898- image.setDotsPerMeterX ( printResolution () / 25.4 * 1000 );
2899- image.setDotsPerMeterY ( printResolution () / 25.4 * 1000 );
2914+ image.setDotsPerMeterX ( resolution / 25.4 * 1000 );
2915+ image.setDotsPerMeterY ( resolution / 25.4 * 1000 );
29002916 image.fill ( 0 );
29012917 QPainter imagePainter ( &image );
29022918 renderPage ( &imagePainter, page );
@@ -2905,15 +2921,32 @@ QImage QgsComposition::printPageAsRaster( int page )
29052921 return image;
29062922}
29072923
2908- QImage QgsComposition::renderRectAsRaster ( const QRectF& rect )
2924+ QImage QgsComposition::renderRectAsRaster ( const QRectF& rect, const QSize& imageSize, int dpi )
29092925{
2910- int width = ( int )( printResolution () * rect.width () / 25.4 );
2911- int height = ( int )( printResolution () * rect.height () / 25.4 );
2926+ int resolution = mPrintResolution ;
2927+ if ( imageSize.isValid () )
2928+ {
2929+ // output size in pixels specified, calculate resolution using average of
2930+ // derived x/y dpi
2931+ resolution = ( imageSize.width () / rect.width ()
2932+ + imageSize.height () / rect.height () ) / 2.0 * 25.4 ;
2933+ }
2934+ else if ( dpi > 0 )
2935+ {
2936+ // dpi overridden by function parameters
2937+ resolution = dpi;
2938+ }
2939+
2940+ int width = imageSize.isValid () ? imageSize.width ()
2941+ : ( int )( resolution * rect.width () / 25.4 );
2942+ int height = imageSize.isValid () ? imageSize.height ()
2943+ : ( int )( resolution * rect.height () / 25.4 );
2944+
29122945 QImage image ( QSize ( width, height ), QImage::Format_ARGB32 );
29132946 if ( !image.isNull () )
29142947 {
2915- image.setDotsPerMeterX ( printResolution () / 25.4 * 1000 );
2916- image.setDotsPerMeterY ( printResolution () / 25.4 * 1000 );
2948+ image.setDotsPerMeterX ( resolution / 25.4 * 1000 );
2949+ image.setDotsPerMeterY ( resolution / 25.4 * 1000 );
29172950 image.fill ( Qt::transparent );
29182951 QPainter imagePainter ( &image );
29192952 renderRect ( &imagePainter, rect );
0 commit comments