1

I'm trying to print my Windows Form. My solution is:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

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

    Bitmap bmp;

    private void button1_Click(object sender, EventArgs e)
    {
        Graphics g = this.CreateGraphics();
        bmp = new Bitmap(this.Size.Width, this.Size.Height, g);
        Graphics mg = Graphics.FromImage(bmp);
        mg.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
        printDialog1.ShowDialog();
        printDocument1.Print();
        printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
    }
}

But it gives me this result:

Windows Form print issue (frame is made by me to show the borders of the printed paper)

How can i solve this problem?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Fth
  • 105
  • 2
  • 19

1 Answers1

0

try this

private void button1_Click(object sender, EventArgs e)
{
    //Add a Panel control.
    Panel panel = new Panel();
    this.Controls.Add(panel);

    //Create a Bitmap of size same as that of the Form.
    Graphics grp = panel.CreateGraphics();
    Size formSize = this.ClientSize;
    bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
    grp = Graphics.FromImage(bitmap);

    //Copy screen area that that the Panel covers.
    Point panelLocation = PointToScreen(panel.Location);
    grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);

    //Show the Print Preview Dialog.
    printPreviewDialog1.Document = printDocument1;
    printPreviewDialog1.PrintPreviewControl.Zoom = 1;
    printPreviewDialog1.ShowDialog();
}

Ref : http://www.aspsnippets.com/Articles/Print-contents-of-Form-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx

Anto sujesh
  • 325
  • 3
  • 14
  • Thanks for the answer :) But it didn't work. It just zooms in in the form and the output is almost the same as my first output, but with the difference that it is zoomed in. – Fth Oct 30 '16 at 20:44