8

Possible Duplicate:
How can I take a screenshot of a Winforms control/form in C#?

I have a windows form with a list of names and pictures. The list is long and so there is a scroll panel for it. Now, I would like to print this form but I can't because the the print function only prints the "visible" portion since the invisible portion is seen when you scroll down. So, is there a way to print the whole form at once?

Community
  • 1
  • 1
Stranger
  • 864
  • 4
  • 21
  • 48
  • 3
    It's not an exact dupe, but the problem is fundamentally the same. There's no easy solution here. What you want to print doesn't actually exist (as it hasn't been drawn). You would have to scroll the form and capture multiple images before printing. It might be easier just to print the data without worrying too much about matching the form exactly. – Jon B Dec 31 '12 at 16:26
  • 1
    @JonB - well, this kind of question could help find a general answer to this recurrent problem. The possible duplicate doesn't seem to apply really well. Is "it's not possible" the only answer ever to this on SO? – Simon Mourier Dec 31 '12 at 16:44

2 Answers2

3

Look for the Print form control in the Visual Basic PowerPacks toolbox

To print the complete client area of a scrollable form try this...

1.In the Toolbox, click the Visual Basic PowerPacks tab and then drag the PrintForm component onto the form.

The PrintForm component will be added to the component tray.

2.In the Properties window, set the PrintAction property to PrintToPrinter.

3.Add the following code in the appropriate event handler (for example, in the Click event handler for a Print Button).

1.PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)

Give this a shot and let me know how it works out for you.

Trevor
  • 7,777
  • 6
  • 31
  • 50
  • This is really sick to identify that i can't extend my form's height more than 764. I tried your solution. It doesn't work with large forms. Do you have any other method of doing this for form go more height with scroll. Thanks – Brune Sep 23 '13 at 20:08
2

This is not exactly a complete answer, but here is a piece of code that takes a screenshot (a bitmap) of a scrollable Panel control on a Form. The big drawback is the screen flickers while the screenshot is taken. I have tested it on simple apps, so it may not work in all cases, but that could be a start.

Here is how to use it:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent(); // create a scrollable panel1 component
    }

    private void button1_Click(object sender, EventArgs e)
    {
        TakeScreenshot(panel1, "C:\\mypanel.bmp");
    }
}

And here is the utility:

    public static void TakeScreenshot(Panel panel, string filePath)
    {
        if (panel == null)
            throw new ArgumentNullException("panel");

        if (filePath == null)
            throw new ArgumentNullException("filePath");

        // get parent form (may not be a direct parent)
        Form form = panel.FindForm();
        if (form == null)
            throw new ArgumentException(null, "panel");

        // remember form position
        int w = form.Width;
        int h = form.Height;
        int l = form.Left;
        int t = form.Top;

        // get panel virtual size
        Rectangle display = panel.DisplayRectangle;

        // get panel position relative to parent form
        Point panelLocation = panel.PointToScreen(panel.Location);
        Size panelPosition = new Size(panelLocation.X - form.Location.X, panelLocation.Y - form.Location.Y);

        // resize form and move it outside the screen
        int neededWidth = panelPosition.Width + display.Width;
        int neededHeight = panelPosition.Height + display.Height;
        form.SetBounds(0, -neededHeight, neededWidth, neededHeight, BoundsSpecified.All);

        // resize panel (useless if panel has a dock)
        int pw = panel.Width;
        int ph = panel.Height;
        panel.SetBounds(0, 0, display.Width, display.Height, BoundsSpecified.Size);

        // render the panel on a bitmap
        try
        {
            Bitmap bmp = new Bitmap(display.Width, display.Height);
            panel.DrawToBitmap(bmp, display);
            bmp.Save(filePath);
        }
        finally
        {
            // restore
            panel.SetBounds(0, 0, pw, ph, BoundsSpecified.Size);
            form.SetBounds(l, t, w, h, BoundsSpecified.All);
        }
    }
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298