Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GDI+ printing
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)
        {
            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)
        {
           //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)
            {
                    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.
Posted 29-Nov-12 23:54pm

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 30 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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