C#Windows窗体打印 - 打印错误的屏幕区域

时间:2016-10-30 17:34:11

标签: c# winforms printing gdi+

我试图打印我的Windows窗体。我的解决方案是:

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);
    }
}

但它给了我这个结果:

Windows Form print issue (框架由我制作,以显示打印纸的边框)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

试试这个

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();
}

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