Slow printing in C#/.NET...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arif

    Slow printing in C#/.NET...

    I C# code prints very slow as compared to a third party barcode printing
    software. That software prints approximately 10 labels in 2 seconds while my
    C# code prints 10 labels in 5 to 6 seconds. And this differences increases
    with the increase number of labels.

    The code is as follwods:
    Here rdr = OleDbDataReader
    Font is Times New Roman, 12pt


    private void printDocument1_ PrintPage(objec t sender,
    System.Drawing. Printing.PrintP ageEventArgs e)
    {
    e.HasMorePages = false;

    if(this.rdr.Rea d())
    {
    e.Graphics.Draw String(rdr[0].ToString(), font, Brushes.Black, 10, 20);
    e.Graphics.Draw String(rdr[1].ToString(), font, Brushes.Black, 10, 40);
    e.Graphics.Draw String(rdr[2].ToString(), font, Brushes.Black, 10, 60);

    if( --this.labels_to_ print > 0)
    e.HasMorePages = true;
    }
    }

    How can I increase printing speed? Please help.

    Arif.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Slow printing in C#/.NET...

    Arif,

    I don't see much room for improvement here. It looks like you are
    disposing of resources correctly (or rather, not allocating anything that
    you don't need during the printing process).

    The Graphics object takes advantage of GDI+, which is very slow. This
    is probably the issue. I would suspect that the third-party component is
    not using GDI+. Why not just use that?

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m
    "Arif" <Arif@discussio ns.microsoft.co m> wrote in message
    news:F2C42C11-2673-4364-9A3A-BF876263C78E@mi crosoft.com...[color=blue]
    >I C# code prints very slow as compared to a third party barcode printing
    > software. That software prints approximately 10 labels in 2 seconds while
    > my
    > C# code prints 10 labels in 5 to 6 seconds. And this differences increases
    > with the increase number of labels.
    >
    > The code is as follwods:
    > Here rdr = OleDbDataReader
    > Font is Times New Roman, 12pt
    >
    >
    > private void printDocument1_ PrintPage(objec t sender,
    > System.Drawing. Printing.PrintP ageEventArgs e)
    > {
    > e.HasMorePages = false;
    >
    > if(this.rdr.Rea d())
    > {
    > e.Graphics.Draw String(rdr[0].ToString(), font, Brushes.Black, 10, 20);
    > e.Graphics.Draw String(rdr[1].ToString(), font, Brushes.Black, 10, 40);
    > e.Graphics.Draw String(rdr[2].ToString(), font, Brushes.Black, 10, 60);
    >
    > if( --this.labels_to_ print > 0)
    > e.HasMorePages = true;
    > }
    > }
    >
    > How can I increase printing speed? Please help.
    >
    > Arif.[/color]


    Comment

    • Craig Scheets

      #3
      Re: Slow printing in C#/.NET...

      Another issue is that you're actually reading directly out of the database
      while printing, and using OleDb (Access?) mind you -- not a good performer.

      Could you read the data into an array first, then call the Print() and just
      iterate through the array inside the PrintPage event? You could then
      isolate the performance problems between reading from the db and how long
      the GDI+ calls and spooling is actually taking. I would put good money that
      your db access is slowing you down MUCH more than GDI+.

      I have an app that prints Legal (8.5x14) paychecks and I can spool about 10
      checks/pages per second on a 2.4Ghz/512MB laptop. They have at least 100
      DrawString()s, probably a dozen fonts (including a custom MICR font -
      similar to a barcode), at least 30-40 DrawLine()s, and 4 DrawImage()s for
      signature files and logos on the checks. Everything is read into memory
      before I call the Print() method though.

      Craig


      "Arif" <Arif@discussio ns.microsoft.co m> wrote in message
      news:F2C42C11-2673-4364-9A3A-BF876263C78E@mi crosoft.com...[color=blue]
      >I C# code prints very slow as compared to a third party barcode printing
      > software. That software prints approximately 10 labels in 2 seconds while
      > my
      > C# code prints 10 labels in 5 to 6 seconds. And this differences increases
      > with the increase number of labels.
      >
      > The code is as follwods:
      > Here rdr = OleDbDataReader
      > Font is Times New Roman, 12pt
      >
      >
      > private void printDocument1_ PrintPage(objec t sender,
      > System.Drawing. Printing.PrintP ageEventArgs e)
      > {
      > e.HasMorePages = false;
      >
      > if(this.rdr.Rea d())
      > {
      > e.Graphics.Draw String(rdr[0].ToString(), font, Brushes.Black, 10, 20);
      > e.Graphics.Draw String(rdr[1].ToString(), font, Brushes.Black, 10, 40);
      > e.Graphics.Draw String(rdr[2].ToString(), font, Brushes.Black, 10, 60);
      >
      > if( --this.labels_to_ print > 0)
      > e.HasMorePages = true;
      > }
      > }
      >
      > How can I increase printing speed? Please help.
      >
      > Arif.[/color]


      Comment

      • Bruce Wood

        #4
        Re: Slow printing in C#/.NET...

        I would recommend that you get your hands on a profiler (comes with
        VS2005, for VS2003 you can use CompuWare's DevPartner Community
        Edition:



        Profile your application, figure out where it's spending all its time,
        and then figure out how to optimize that.

        Comment

        • Arif

          #5
          Re: Slow printing in C#/.NET...

          Now i am using the simplest code that reads the data from an array rather
          than from OleDbDatareader object, as follows:

          private void printDocument1_ PrintPage(objec t sender,
          System.Drawing. Printing.PrintP ageEventArgs e)
          {
          e.Graphics.Draw String(this.dat a_ary[this.labels_to_ print-1, 0], font,
          Brushes.Black, 10, 20);
          e.Graphics.Draw String(this.dat a_ary[this.labels_to_ print-1, 1], font,
          Brushes.Black, 10, 40);
          e.Graphics.Draw String(this.dat a_ary[this.labels_to_ print-1, 2], font,
          Brushes.Black, 10, 60);

          if( --this.labels_to_ print > 0)
          e.HasMorePages = true;
          else
          e.HasMorePages = false;

          }

          But I am seeing the same down printing speed as was in the case using
          OleDBDataReader instead of an array.

          I also notice that when I turn off the printer,click to print then 64
          pages/labels are spooled very fast. But if the printer is ON then this
          spooling is comparatively slow.

          I think that the printer is printing labels perhaps as a separate print job
          for each page because there is a step/0.5 second delay between two labels
          printing. But the third party software prints contineously and very fast.
          when I used the following code to print 64 labels separately, I see the same
          printing style/speed as was when printing 64 labels in on printing job.

          some_methos()
          {
          for(int i=0; i < this.labels_to_ print; i++) //printing 64 labels as separate
          print job.
          this.printDocum ent1.Print();
          }

          private void printDocument1_ PrintPage(objec t sender,
          System.Drawing. Printing.PrintP ageEventArgs e)
          {
          e.HasMorePages = false;

          e.Graphics.Draw String(this.dat a_ary[this.labels_to_ print-1, 0], font,
          Brushes.Black, 10, 20);
          e.Graphics.Draw String(this.dat a_ary[this.labels_to_ print-1, 1], font,
          Brushes.Black, 10, 40);
          e.Graphics.Draw String(this.dat a_ary[this.labels_to_ print-1, 2], font,
          Brushes.Black, 10, 60);
          }

          I think that may be there some settings for printer that I should take care
          in C# code.

          Any new idea, please share.

          Arif.


          "Craig Scheets" wrote:
          [color=blue]
          > Another issue is that you're actually reading directly out of the database
          > while printing, and using OleDb (Access?) mind you -- not a good performer.
          >
          > Could you read the data into an array first, then call the Print() and just
          > iterate through the array inside the PrintPage event? You could then
          > isolate the performance problems between reading from the db and how long
          > the GDI+ calls and spooling is actually taking. I would put good money that
          > your db access is slowing you down MUCH more than GDI+.
          >
          > I have an app that prints Legal (8.5x14) paychecks and I can spool about 10
          > checks/pages per second on a 2.4Ghz/512MB laptop. They have at least 100
          > DrawString()s, probably a dozen fonts (including a custom MICR font -
          > similar to a barcode), at least 30-40 DrawLine()s, and 4 DrawImage()s for
          > signature files and logos on the checks. Everything is read into memory
          > before I call the Print() method though.
          >
          > Craig
          >
          >
          > "Arif" <Arif@discussio ns.microsoft.co m> wrote in message
          > news:F2C42C11-2673-4364-9A3A-BF876263C78E@mi crosoft.com...[color=green]
          > >I C# code prints very slow as compared to a third party barcode printing
          > > software. That software prints approximately 10 labels in 2 seconds while
          > > my
          > > C# code prints 10 labels in 5 to 6 seconds. And this differences increases
          > > with the increase number of labels.
          > >
          > > The code is as follwods:
          > > Here rdr = OleDbDataReader
          > > Font is Times New Roman, 12pt
          > >
          > >
          > > private void printDocument1_ PrintPage(objec t sender,
          > > System.Drawing. Printing.PrintP ageEventArgs e)
          > > {
          > > e.HasMorePages = false;
          > >
          > > if(this.rdr.Rea d())
          > > {
          > > e.Graphics.Draw String(rdr[0].ToString(), font, Brushes.Black, 10, 20);
          > > e.Graphics.Draw String(rdr[1].ToString(), font, Brushes.Black, 10, 40);
          > > e.Graphics.Draw String(rdr[2].ToString(), font, Brushes.Black, 10, 60);
          > >
          > > if( --this.labels_to_ print > 0)
          > > e.HasMorePages = true;
          > > }
          > > }
          > >
          > > How can I increase printing speed? Please help.
          > >
          > > Arif.[/color]
          >
          >
          >[/color]

          Comment

          Working...