在C#中打印一个表单/用户控件

6

我的程序: 包含一个带有几个文本框和一个按钮的表单。在我的电脑上,“默认打印机”设置为 Adobe PDF

我的目标: 当用户点击“打印”按钮时,想要对表单/用户控件进行屏幕截图。然后将截图保存在桌面上,以 .pdf 格式保存。

我的问题: 我的代码有以下两个问题:

  1. 屏幕截图尺寸: 屏幕截图的尺寸太大,无法适应页面大小 (默认页面大小) 在打印/转换为 .pdf 时。请参考下面的两张图片。我希望整个屏幕截图都能适应页面。
  2. 询问两次转换和保存位置: 当我点击“打印表单”按钮时,程序会两次询问我要在哪里打印/转换并保存文件。我希望程序只询问一次,在哪里打印并保存文件。

问题 1: 程序捕获的屏幕截图打印时不适合页面。 Problem 1: The screenshot captured by the program does not fit the page when printed.

我希望屏幕截图可以像这样适合一个 .pdf 页面: enter image description here

代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        button1.Text = "Print Form";
        button1.Click += new EventHandler(button1_Click);
        printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
        this.Controls.Add(button1);
    }

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

    Bitmap memoryImage;

    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(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }
}

提前感谢您的帮助。我是一个新手,正在学习C#编程语言,非常感谢您的帮助。 :)


1
看一下这个问题/答案,了解C#图像缩放的示例:https://dev59.com/iXVC5IYBdhLWcg3wliGe - Paul Sasik
对于第二个问题(双重对话框),猜测是因为您的构造函数中有这行代码 printDocument1.PrintPage += ... ,请尝试将其删除。我猜这个事件已经在 InitializeComponent() 中触发了一次,也就是说你显式地处理了两次事件。打开 Form1.Designer.cs 文件查看是否还有其他重复的内容。(除了 Init 调用之外,构造函数中的其他全部可能都是多余的。) - Paul Sasik
@PaulSasik:不行。对于问题2,删除那一行会给我一个白色的PDF文件,无法保存。._. - Smith
请查看下面我的答案。首先只需从类中复制/粘贴printDocument1_PrintPage方法。要处理重复的“保存对话框”,请查看整个代码。(事件通常在相关的设计器类文件中管理窗体。) - Paul Sasik
1个回答

3

好的,看一下这个,特别是修改过的printDocument1_PrintPage

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            // calculate width and height scalings taking page margins into account
            var wScale = e.MarginBounds.Width / (float)_memoryImage.Width;
            var hScale = e.MarginBounds.Height / (float)_memoryImage.Height;

            // choose the smaller of the two scales
            var scale = wScale < hScale ? wScale : hScale;

            // apply scaling to the image
            e.Graphics.ScaleTransform(scale, scale);

            // print to default printer's page
            e.Graphics.DrawImage(_memoryImage, 0, 0);
        }

我将所有的事件绑定移动到InitializeComponent中,这是它通常应该放置的地方,但是它需要更多涉及的代码:

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

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

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

        private Bitmap _memoryImage;

        private void CaptureScreen()
        {
            // put into using construct because Graphics objects do not 
            //  get automatically disposed when leaving method scope
            using (var myGraphics = CreateGraphics())
            {
                var s = Size;
                _memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
                using (var memoryGraphics = Graphics.FromImage(_memoryImage))
                {
                    memoryGraphics.CopyFromScreen(Location.X, Location.Y, 0, 0, s);
                }
            }
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            // calculate width and height scalings taking page margins into account
            var wScale = e.MarginBounds.Width / (float)_memoryImage.Width;
            var hScale = e.MarginBounds.Height / (float)_memoryImage.Height;

            // choose the smaller of the two scales
            var scale = wScale < hScale ? wScale : hScale;

            // apply scaling to the image
            e.Graphics.ScaleTransform(scale, scale);

            // print to default printer's page
            e.Graphics.DrawImage(_memoryImage, 0, 0);
        }
    }
}

Form1.Designer.cs

namespace testScreenCapScale
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
                components.Dispose();
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.printDocument1 = new System.Drawing.Printing.PrintDocument();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // printDocument1
            // 
            this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(64, 220);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(384, 377);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        private System.Drawing.Printing.PrintDocument printDocument1;
        private System.Windows.Forms.Button button1;
    }
}

@Smith:就像我说的,作为一个入门测试,只需复制并粘贴printDocument1_PrintPage方法的内容。缩放是自包含的,不需要改变其他任何东西。更复杂的是事件处理,您可以单独处理。 - Paul Sasik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接