1

I have a long form that needs to be printed. The form itself can be larger than what is displayed on screen (the height is unknown until data is loaded into the form). I have tried several methods but none has worked.

Below is the closest method to what I'm trying to accomplish. The problem with the method below is that it only prints the part of the screen. I'm also not sure if it captures the full form or just what is visible on the user's screen.

    private void print_Click(object sender, EventArgs e)
    {
        PrintScreen();
        DialogResult result = printDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            printDocument1.Print();
        }

    }

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);


    private void PrintScreen()
    {          
        Graphics mygraphics = this.CreateGraphics();
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        IntPtr dc1 = mygraphics.GetHdc();
        IntPtr dc2 = memoryGraphics.GetHdc();
        BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
        mygraphics.ReleaseHdc(dc1);
        memoryGraphics.ReleaseHdc(dc2);

    }

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }

Please help.

10bit
  • 83
  • 8
  • 1
    Have you considered creating a PDF? This is more work but gives you a nicer output and would solve your issue. – Thane Plummer Jul 29 '15 at 14:10
  • @ThanePlummer No I haven't thought of that. I'll look into it. If anyone has any suggestions, or useful links please let me know. – 10bit Jul 29 '15 at 14:18
  • We use iTextSharp (free) and TallPDF (commercial) for our projects. IMO iTextSharp is harder to work with but not bad if you want simple output. TallPDF can handle complex output, is very fast, but is also expensive. – Thane Plummer Jul 29 '15 at 14:23
  • 1
    Getting the full form into a bitmap is [not so hard](http://stackoverflow.com/questions/30918450/winforms-how-to-print-the-whole-of-a-form-including-unseen-parts/30924473?s=1|0.0000#30924473) but unless you have retina displays it will look rather bad on pretty much any printer. You really should recreate the printed version in its own routine and with a decent resolution. Prepare yourself for a lot of work no matter if you do it directly in GDI+ or via PDF.. – TaW Jul 29 '15 at 19:00
  • @DourHighArch I mean a winform. For example the user selects a order and clicks view order, which takes him to a new form that gives details about the order. Then he clicks print and it would print the details form. – 10bit Jul 30 '15 at 10:53

1 Answers1

1

If you are trying to print the contents of a WinForm to a printer, drawing UI controls to the printer is not going to work. You can't resize or scroll a piece of paper like you can a screen. You will find that what looks good on screen does not look good on paper; fonts are pixelated, gradients are dithered, text gets cut off, layout that looks good on screen will print at crazy locations on paper.

You will want to use the System.Drawing.Printing classes to determine the printer metrics like page size, color, margins and so on. Then, divide your output into text paragraphs or graphic tiles that fit into the page. If a paragraph or tile overflows a page, decide how to split it and print a section on multiple pages. The PrintDocument documentation has an example of printing over multiple pages.

For anything but the simplest output this is going to be a lot of work. You may be better off using a reporting engine like Report Viewer or nReports to generate PDF or RDL output and send that to a printer.

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90