0

I have a service which prints. Up until now the service has been printing using the WPF System.Windows.Controls.PrintDialog.PrintDocument method however due to various issues (performance, windows update bugs, 32 bit service on 64bit system issues etc) I am converting to use the traditional System.Drawing.Printing.PrintDocument method.

As this is running as a service I want this to always print using the default printer preferences (which include things like media settings and print speed for industrial label printer such as the Intermec/Honewell PM43)

Previously I had done this using PrintDialog.PrintTicket = PrintQueue.DefaultPrintTicket.Clone

However I cannot find the equivalent method in System.Drawing.Printing.PrintDocument and the service is not picking up the default printer preferences as set in the printer properties (specifically Print Speed in this case)

So what is the equivalent of PrintDialog.PrintTicket = PrintQueue.DefaultPrintTicket.Clone in System.Drawing.Printing.PrintDocument?

apc
  • 5,306
  • 1
  • 17
  • 26

1 Answers1

0

The following question helped me find an answer Can't change DEVMODE of a printer

//Get the registry key containing the printer settings
var regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Control\\Print\\Printers\\" + PrinterName);
if (regKey != null)
{    
    //Get the value of the default printer preferences
    var defaultDevMode = (byte[])regKey.GetValue("Default DevMode");
    //Create a handle and populate
    var pDevMode = Marshal.AllocHGlobal(defaultDevMode.Length);
    Marshal.Copy(defaultDevMode, 0, pDevMode, defaultDevMode.Length);
    //Set the printer preferences
    pd.PrinterSettings.SetHdevmode(pDevMode);
    //Clean up
    Marshal.FreeHGlobal(pDevMode);
}
apc
  • 5,306
  • 1
  • 17
  • 26