Click here to Skip to main content
13,594,513 members
Rate this:
Please Sign up or sign in to vote.
See more:
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;

    //igra oko traženja pisača
    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)
    pd.PrinterSettings.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(50, 50, 50, 50);
    pd.PrinterSettings.DefaultPageSettings.Landscape = true;
    PlanoviPrintPreviewForm ppForm = null;

    while (true)
            ppForm = new PlanoviPrintPreviewForm(pd);
            ppForm .ShowDialog();
        catch (Exception ex)
           //oskimoron, ali neka ostane dok se sve ne izdebagira
           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)
                throw ex;

    ppForm .Dispose();

This line of code is main suspect:

_margineBounds = new Rectangle(_margine.Left, 
                               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.
Posted 29-Nov-12 22:54pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03-2016 | 2.8.180621.3 | Last Updated 30 Nov 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100