@@ -2887,16 +2887,32 @@ bool QgsComposition::print( QPrinter &printer, const bool evaluateDDPageSize )
2887
2887
return true ;
2888
2888
}
2889
2889
2890
- QImage QgsComposition::printPageAsRaster ( int page )
2890
+ QImage QgsComposition::printPageAsRaster ( int page, const QSize& imageSize, int dpi )
2891
2891
{
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
+
2895
2911
QImage image ( QSize ( width, height ), QImage::Format_ARGB32 );
2896
2912
if ( !image.isNull () )
2897
2913
{
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 );
2900
2916
image.fill ( 0 );
2901
2917
QPainter imagePainter ( &image );
2902
2918
renderPage ( &imagePainter, page );
@@ -2905,15 +2921,32 @@ QImage QgsComposition::printPageAsRaster( int page )
2905
2921
return image;
2906
2922
}
2907
2923
2908
- QImage QgsComposition::renderRectAsRaster ( const QRectF& rect )
2924
+ QImage QgsComposition::renderRectAsRaster ( const QRectF& rect, const QSize& imageSize, int dpi )
2909
2925
{
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
+
2912
2945
QImage image ( QSize ( width, height ), QImage::Format_ARGB32 );
2913
2946
if ( !image.isNull () )
2914
2947
{
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 );
2917
2950
image.fill ( Qt::transparent );
2918
2951
QPainter imagePainter ( &image );
2919
2952
renderRect ( &imagePainter, rect );
1 commit comments
agiudiceandrea commentedon Feb 28, 2018
Hi @nyalldawson,
it seems to me that this commit introduced a problem when exporting images as raster. Could you please take a look at the bug report 18132?