Skip to content

Instantly share code, notes, and snippets.

@corytodd
Created June 13, 2018 02:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save corytodd/b9b04324aafc5a5124d2576bbe255abd to your computer and use it in GitHub Desktop.
Save corytodd/b9b04324aafc5a5124d2576bbe255abd to your computer and use it in GitHub Desktop.
GDI Printing Example for C#
#region Win32 GDI
private void btnTextDrawCenter_Click(object sender, RoutedEventArgs e)
{
var doc = new PrintDocument()
{
PrintController = new StandardPrintController(),
};
doc.OriginAtMargins = false;
doc.PrinterSettings.PrinterName = CurrentPrinter;
doc.PrintPage += (s, args) =>
{
var bounds = args.Graphics.VisibleClipBounds;
bounds.Width *= args.Graphics.DpiX / 96.0f;
var largeFont = new System.Drawing.Font("Consolas", 72.0f);
System.Drawing.Size strSz;
using (var renderer = new NativeTextRenderer(args.Graphics))
{
var str = "█<-1\"->█";
renderer.DrawString(str,
largeFont,
Color.Black,
bounds,
RawPrinterHelper.TextFormatFlags.Center);
strSz = renderer.MeasureString(str, largeFont);
bounds.Y += strSz.Height;
}
using (var renderer = new NativeTextRenderer(args.Graphics))
{
var str = string.Format("bounds: {0}\nStrSize: {0}", bounds.ToString(), strSz.ToString());
renderer.DrawString(str,
new System.Drawing.Font("Consolas", 12.0f),
Color.Black,
bounds,
0);
}
};
doc.Print();
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment