1

I have a win form where i have some label,a datagridview and some textbox.I want to print all of my items in my form in exact format it shows in the form. I have tried it using taking snap of my form then print it. But in this way i can't have my full form as my form is large enough.It only shows half portion of my form.I don't know is it the best practice on not.Is there any other way of doing this please mention

My Sample code:

private void btnPrint_Click(object sender, EventArgs e)
    {
        CaptureScreen();
        printDocument1.Print();
    }

private void CaptureScreen()
    {
        Graphics myGraphics = this.CreateGraphics();
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
    }

 private void printDocument1_PrintPage_1(object sender,  PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }

As i am new in C#,it will be great help if anyone help me with some sample code.

Bashar
  • 109
  • 3
  • 13

1 Answers1

0

I think you should emulate your form layout in a page or text description language like PDF or RTF. Taking a screenshot programmatically is in a way elegant but also crude at the same time, and runs into limitations of which you encountered just one.

One thing to consider is whether you should serialize your data e.g. as XML first, and only then export to data formats suitable for printing or displaying, like PDF or HTML.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
  • How can i emulate my form layout in PDF ? – Bashar Apr 23 '15 at 15:32
  • That is a good question. I meant to format the information that you display in the form as text. Even ASCII is a first step. Then something like `a2ps -o -|ps2pdf` (under cygwin here) seems to work for ASCII text, producing PDF which preserves a simple layout. Of course fancy fonts, colors etc are all not preserved. Apparently there are also XML to PDF converters, if I can believe google. Generally I would look at your data in an abstract way (away from a C# form) and consider the C# form as just *one* of many ways to display it. – Peter - Reinstate Monica Apr 23 '15 at 15:55