For obtaining the true image size in printer's points, one should wrap the expression with Annotation
and use Rasterize[..., "Regions"]
to get its bounding rectangle:
Clear[expr, imageSize]
expr[h_, opts : OptionsPattern[]] :=
Graphics[{Red, Rectangle[{0, 0}, {200, h}]}, ImageSize -> 200, opts];
imageSize[expr_] :=
Differences[Rasterize[Annotation[expr, "out", "Region"], "Regions"][[1, 2]]][[1]]
imageSize[expr[300]]
{200., 295.68}
(It is possible to use ResourceFunction["GraphicsInformation"]
or ResourceFunction["CellBoundingRectangle"]
for obtaining the same information.)
As you can see, the vertical size is slightly different than expected. I believe this is a bug in Graphics
rendering. To get the expected size, you need to specify the option PlotRangePadding -> None
for Graphics
:
imageSize[expr[300, PlotRangePadding -> None]]
{200., 300.}
With the other possible choices for the height you also get the expected result:
Table[imageSize[expr[h, PlotRangePadding -> None]], {h, {300, 200, 100, 10, 1}}]
{{200., 300.}, {200., 200.}, {200., 100.}, {200., 10.}, {200., 1.}}
You can check what image size you obtain on exporting to a vector graphics format:
vectorExportImportPDF[expr_] :=
First@ImportString[ExportString[expr, "PDF", "AllowRasterization" -> False],
If[$VersionNumber >= 12.2, {"PDF", "PageGraphics"}, {"PDF", "Pages"}],
"TextOutlines" -> False]
vectorExportImportPDF[expr[300, PlotRangePadding -> None]] // Options
{ImageSize -> {200., 300.}, PlotRange -> {{0., 200.}, {0., 300.}}, AspectRatio -> Automatic}
The final rendered image size in pixels depends on the resolution of your monitor and local Magnification
setting of a Notebook where the graphics is rendered. The pixel size should be equal to the image size in printer's poins times magnification*resolution/72.
, where resolution
of your monitor can be obtained from SystemInformation
:
resolution = "Resolution" /. First[SystemInformation["Devices", "ScreenInformation"]]
or from CurrentValue["WindowResolution"]
:
resolution = CurrentValue["WindowResolution"]
However, note that prior to Mathematica 13.0, by default on Windows the monitor resolution was always assumed to be 72.
. Since you're using version 12.2 on Windows and SystemInformation
reports a monitor resolution of 120.
, you've probably disabled the compatibility mode via the option "ScreenResolutionCompatibilityMode"
. Starting from version 13.0 the compatibility mode by default is off, and SystemInformation
reports the true resolution of the monitor (taking into account the scaling set at the OS level).
The local Magnification
option by default is inherited from Notebook's Magnification
:
magnification = CurrentValue[EvaluationNotebook[], Magnification]
But in your case for Graphics
(and Image
without explicit Magnification
option) it will be equal to 3/5
because you have set a fixed Magnification
for GraphicsBox
via global GraphicsBoxOptions
:
SetOptions[$FrontEnd, GraphicsBoxOptions -> {BaseStyle -> Magnification -> 3/5}];
Related MMa.SE threads:
Related WC threads: