尝试截屏表单时格式不正确并打印[复制]

问题描述 投票:0回答:1

这个问题在这里已有答案:

晚上好,我目前在打印表格时遇到了一些格式问题。我使用的代码是通过按钮(“打印表单”)激活的,如下所示:

private void printAdmin_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        printAdmin.DefaultPageSettings.Landscape = true;

        e.Graphics.DrawImage(AdminPage,0,0);
    }
    Bitmap AdminPage;
    private void Btn_Admin_Prt_Click(object sender, EventArgs e)
    {
        try
        {

            Graphics g = this.CreateGraphics();
            AdminPage = new Bitmap(Size.Width, Size.Height, g);
            Graphics Printed = Graphics.FromImage(AdminPage);
            Printed.CopyFromScreen(this.Location.X, this.Location.Y,0,0,this.Size);

            printPreviewAdminDialogue.ShowDialog();
        }
        catch(Exception )
        {
            MessageBox.Show("Please check printer connection!");
        }

        //Takes a screenshot of the admin page
        //opens print dialogue
        //prints awesomley
    }

当我运行应用程序时,在打印预览之前没有任何错误,这应该打印表单的整个屏幕截图,但是将其中的一半切掉,看起来像下面的图像。 Image of print preview

关于如何正确格式化这样的想法,以便捕获整个表单。

提前致谢。

c# winforms printing
1个回答
0
投票

这只是测量正确坐标的问题。 您需要一个Graphics对象来捕获屏幕区域并将其传输到Bitmap(实际与Bitmap相关的Graphics对象)。

我不知道你是否只需要ClientArea或整个Form,所以这个方法接受一个bool参数,TheWholeForm,它允许你指定哪一个。

Bitmap AdminPage_Full;
Bitmap AdminPage_ClientArea;

    AdminPage_Full = PrintForm(true);
    AdminPage_ClientArea = PrintForm(false);

    //Test it
    this.pictureBox1.Image = (Bitmap)AdminPage_Full.Clone();
    this.pictureBox2.Image = (Bitmap)AdminPage_ClientArea.Clone();

    private Bitmap PrintForm(bool TheWholeForm)
    {
        using (Bitmap bitmap = new Bitmap(this.Bounds.Width, this.Bounds.Height)) {
            using (Graphics graphics = Graphics.FromImage(bitmap)) {
                if (TheWholeForm) {
                    graphics.CopyFromScreen(this.Bounds.Location, new Point(0, 0), this.Bounds.Size);
                } else {
                    graphics.CopyFromScreen(this.PointToScreen(this.ClientRectangle.Location),
                                            new Point(0, 0), this.ClientRectangle.Size);
                }
                graphics.DrawImage(bitmap, new Point(0, 0));
                return (Bitmap)bitmap.Clone();
            };
        };
    }
© www.soinside.com 2019 - 2024. All rights reserved.