<PackageReference Include="System.Drawing.Common" Version="9.0.0" />

PrintPreviewGraphics

sealed class PrintPreviewGraphics
using System.Drawing.Printing; using System.Runtime.CompilerServices; using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.Graphics.Gdi; namespace System.Drawing { [NullableContext(1)] [Nullable(0)] internal sealed class PrintPreviewGraphics { private readonly PrintPageEventArgs _printPageEventArgs; private readonly PrintDocument _printDocument; public RectangleF VisibleClipBounds { get { HGLOBAL hdevmodeInternal = _printPageEventArgs.PageSettings.PrinterSettings.GetHdevmodeInternal(); CreateDcScope scope = _printPageEventArgs.PageSettings.PrinterSettings.CreateDeviceContext(hdevmodeInternal); try { using (Graphics graphics = Graphics.FromHdcInternal((IntPtr)ref scope)) { if (_printDocument.OriginAtMargins) { int deviceCaps = PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.LOGPIXELSX); int deviceCaps2 = PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.LOGPIXELSY); int deviceCaps3 = PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.PHYSICALOFFSETX); int deviceCaps4 = PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.PHYSICALOFFSETY); float num = (float)(deviceCaps3 * 100 / deviceCaps); float num2 = (float)(deviceCaps4 * 100 / deviceCaps2); graphics.TranslateTransform(0 - num, 0 - num2); graphics.TranslateTransform((float)_printDocument.DefaultPageSettings.Margins.Left, (float)_printDocument.DefaultPageSettings.Margins.Top); } return graphics.VisibleClipBounds; } } finally { scope.Dispose(); } } } public PrintPreviewGraphics(PrintDocument document, PrintPageEventArgs e) { _printPageEventArgs = e; _printDocument = document; } } }