We've made in-house solution several years ago. One of the reports had to be done as GDI+ report.
So I've made it and it worked fine until recently.
Sometimes print preview crashes even though (default) network printer is online.
Code below I use for creating print preview, a you can see I check whether printer is online and only then I proceed to creating preview. Crash happens during OnBeginPrint event, on setting bounds to be more specific.
using (PlanEmitiranjaDocument pd = new PlanEmitiranjaDocument(dataSet, _vrstaPlana))
{
this.Cursor = Cursors.WaitCursor;
bool breakOuter = false;
pd.Header.Font = new Font(pd.Header.Font.FontFamily, 15, FontStyle.Bold);
pd.Header.Visible = pd.Footer.Visible = true;
pd.Footer.Aligment = StringAlignment.Center;
pd.PrinterSettings = new PrinterSettings();
bool? imaPisaca = Helper.IsPrinterOnline(pd.PrinterSettings.PrinterName);
PrintDialog printDialog = new PrintDialog();
if (!imaPisaca.HasValue || !imaPisaca.Value)
{
printDialog.Document = pd;
if (printDialog.ShowDialog() != DialogResult.OK)
{
break;
}
}
pd.PrinterSettings.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(50, 50, 50, 50);
pd.PrinterSettings.DefaultPageSettings.Landscape = true;
PlanoviPrintPreviewForm ppForm = null;
while (true)
{
try
{
ppForm = new PlanoviPrintPreviewForm(pd);
ppForm .ShowDialog();
break;
}
catch (Exception ex)
{
if (ex.Message.Contains("No printers are installed"))
{
MessageBox.Show("FireAd ne prepoznaje odabrani pisač. Pokušajte odabrati drugi pisač kao osnovni (default).\n" +
"Upozorenje", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (printDialog.ShowDialog() != DialogResult.OK)
{
break;
}
}
else
{
throw ex;
}
}
}
ppForm .Dispose();
}
This line of code is main suspect:
_margineBounds = new Rectangle(_margine.Left,
_margine.Top,
this.DefaultPageSettings.Bounds.Width - 2*_margine.Left, this.DefaultPageSettings.Bounds.Height - _margine.Top - _margine.Bottom);
This is stack trace if it helps:
at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal(String printer)
at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal()
at System.Drawing.Printing.PrinterSettings.GetHdevmode()
at System.Drawing.Printing.PageSettings.get_Bounds()
at ReklameModule.UI.PlanEmitiranja.PlanEmitiranjaDocument.NamjestiDefaultneVelicine()
at ReklameModule.UI.PlanEmitiranja.PlanEmitiranjaDocument.OnBeginPrint(PrintEventArgs e)
at System.Drawing.Printing.PrintDocument._OnBeginPrint(PrintEventArgs e)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
at System.Windows.Forms.PrintPreviewControl.ComputePreview()
at System.Windows.Forms.PrintPreviewControl.CalculatePageInfo()
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
At the end I wonder how it is possible to throw
No printers are installed exception?!
And yes, we've reinstalled printer.