PreviewPrintController
Specifies a print controller that displays a document on a screen as a series of images.
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Runtime.CompilerServices;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
namespace System.Drawing.Printing
{
[NullableContext(1)]
[Nullable(0)]
public class PreviewPrintController : PrintController
{
[Nullable(2)]
private Graphics _graphics;
[Nullable(2)]
private HdcHandle _hdc;
private readonly List<PreviewPageInfo> _list = new List<PreviewPageInfo>();
public override bool IsPreview => true;
public virtual bool UseAntiAlias { get; set; }
public PreviewPageInfo[] GetPreviewPageInfo()
{
return _list.ToArray();
}
public override void OnStartPrint(PrintDocument document, PrintEventArgs e)
{
base.OnStartPrint(document, e);
if (!document.PrinterSettings.IsValid)
throw new InvalidPrinterException(document.PrinterSettings);
PrinterSettings printerSettings = document.PrinterSettings;
SafeDeviceModeHandle modeHandle = _modeHandle;
_hdc = new HdcHandle(printerSettings.CreateInformationContext((modeHandle != null) ? ((HGLOBAL)modeHandle) : HGLOBAL.Null));
}
public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)
{
base.OnStartPage(document, e);
if (e.CopySettingsToDevMode)
e.PageSettings.CopyToHdevmode(_modeHandle);
Size size = e.PageBounds.Size;
Size size2 = PrinterUnitConvert.Convert(size, PrinterUnit.Display, PrinterUnit.HundredthsOfAMillimeter);
HdcHandle handle = _hdc;
HDC hDC = (handle != null) ? ((HDC)ref handle) : HDC.Null;
Metafile image = new Metafile(hDC, new Rectangle(0, 0, size2.Width, size2.Height), MetafileFrameUnit.GdiCompatible, EmfType.EmfPlusOnly);
PreviewPageInfo item = new PreviewPageInfo(image, size);
_list.Add(item);
PrintPreviewGraphics printingHelper = new PrintPreviewGraphics(document, e);
_graphics = Graphics.FromImage(image);
if (document.OriginAtMargins) {
int deviceCaps = PInvokeCore.GetDeviceCaps(hDC, GET_DEVICE_CAPS_INDEX.LOGPIXELSX);
int deviceCaps2 = PInvokeCore.GetDeviceCaps(hDC, GET_DEVICE_CAPS_INDEX.LOGPIXELSY);
int deviceCaps3 = PInvokeCore.GetDeviceCaps(hDC, GET_DEVICE_CAPS_INDEX.PHYSICALOFFSETX);
int deviceCaps4 = PInvokeCore.GetDeviceCaps(hDC, GET_DEVICE_CAPS_INDEX.PHYSICALOFFSETY);
float num = (float)deviceCaps3 * 100 / (float)deviceCaps;
float num2 = (float)deviceCaps4 * 100 / (float)deviceCaps2;
_graphics.TranslateTransform(0 - num, 0 - num2);
_graphics.TranslateTransform((float)document.DefaultPageSettings.Margins.Left, (float)document.DefaultPageSettings.Margins.Top);
}
_graphics.PrintingHelper = printingHelper;
if (UseAntiAlias) {
_graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
_graphics.SmoothingMode = SmoothingMode.AntiAlias;
}
return _graphics;
}
public override void OnEndPage(PrintDocument document, PrintPageEventArgs e)
{
_graphics?.Dispose();
_graphics = null;
base.OnEndPage(document, e);
}
public override void OnEndPrint(PrintDocument document, PrintEventArgs e)
{
_hdc?.Dispose();
_hdc = null;
base.OnEndPrint(document, e);
}
}
}