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

2024-04-12

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

My Goal:想要在用户单击“打印”按钮时截取表单/用户控件的屏幕截图。然后屏幕截图将以 .pdf 格式保存在桌面上。

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

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

Problem 1: The screenshot captured by the program does not fit the page when printed. Problem 1: The screenshot captured by the program does not fit the page when printed.

I want the screenshot image to fit like this on one page of .pdf: enter image description here

Code:

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# 语言,非常感谢您的帮助。 :)


好的,检查一下,修改后的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;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 C# 中打印表单/用户控件 的相关文章

随机推荐

  • 在 haskell 中实现 Alpha 等价

    我想使用此数据定义来定义 Alpha 等价 type Sym Char data Exp Var Sym App Term Exp Lam Sym Exp deriving Eq Read Show 做这个的最好方式是什么 一种方法是将名称
  • 如何删除 git 中的本地存储库? [复制]

    这个问题在这里已经有答案了 我找不到命令 我尝试谷歌搜索 git 删除存储库 删除 git如果您只想删除与 git 相关的信息 分支 版本 请在存储库的根目录中删除该目录 如果你想删除所有内容 git data 代码等 只需删除整个目录即可
  • Gradle 包装器在 Windows 上的 Android 项目中获取错误的 Java 版本

    我在 Windows 上的 Android 项目中使用 gradle 当我执行时 gradlew version在我的项目路径中 错误显示为Could not determine java version from 12 我确实有已安装 J
  • 奇怪的错误,set::begin() 总是返回 const 迭代器

    为什么 set begin 总是返回一个 const 迭代器而不是标准迭代器 35 int test 36 std set
  • 在 MySQL 中将“描述”转换为“创建表”?

    我们可以从描述中获取 创建表 命令 describe 吗 我有一个表 其描述可以从 DESC TableName 获得 我想知道我是否可以了解该表是如何创建的 以便我可以使用相同的命令执行其他操作 我可以得到 sql dump 但我想知道是
  • F# 从 while 循环中中断

    有什么方法可以做到这一点C C 例如 C 风格 for int i 0 i lt 100 i if i 66 break 最简洁的答案是不 您通常会使用一些高阶函数来表达相同的功能 有许多函数可以让您执行此操作 对应于不同的模式 因此 如果
  • IP 地址的索引范围搜索算法

    给定一个包含 100 亿个以 CIDR 表示法表示的 IPv4 范围或两个 IP 之间的 ACL 列表 x x x x y x x x x y y y y 用于测试给定 IP 地址是否满足一个或多个 ACL 范围条件的有效搜索 索引算法是什
  • 在 MVP android 应用程序中演示者之间进行通信

    我正在使用 MVP 模式构建一个小型测试 Android 应用程序 我有两个片段片段 B 我用于滑动抽屉 和片段 A 主片段 两个片段都有自己的演示者 当我单击滑动绘制时 它应该发送消息或调用片段 A 中的方法来更新视图 我想问一下 两个片
  • 如何使用智能卡和 python 发出 TLS 请求?

    我尝试使用 python 库 请求 与受智能卡保护的网站进行通信 这意味着 SSL 中的强身份验证 您必须提供客户端证书 证书和私钥 由于我使用的是智能卡 因此我无法读取普通保护的私钥 只能读取模数 我可以使用 python 库 PyKCS
  • Firestore 分别按日期和时间查询

    如何按特定日期和时间范围查询我的文档 IE 我想按特定日期范围 01 01 2019 31 01 2019 查询文档 并且从这些日期仅查询上午 10 点到中午 12 点制作的文档 事情会是这样的 let ref db collection
  • Python将一维数组转换为二维数组[重复]

    这个问题在这里已经有答案了 我有一个清单 1 2 3 4 5 6 7 8 我想在 python 中将其转换为 1 2 3 4 5 6 7 8 有人可以帮我解决这个问题吗 接受输入 def chunks l n return l i i n
  • 声明 DNA 的新数据类型

    我从事生物学研究 特别是 DNA 并且经常存在来自基因组测序的数据大小的问题 对于那些没有生物学背景的人 我将快速概述 DNA 测序 DNA 由四个字母组成 A T G 和 C 它们的具体顺序决定了细胞中发生的情况 然而 DNA 测序技术的
  • GitHub 可以用于托管文件(mp3 和图像)吗?

    我正在寻找免费的文件托管服务 它可以让我获得静态链接到每个单独的文件easily 因此 文件 1 png 2 png 3 png 应分配给 URL www something com somepath 1 png http www some
  • Bourbon/Sass:#{$all-text-inputs} 带有悬停或焦点?

    根据波本文档 http thoughtbot com bourbon html5 input types 您可以使用 all text inputs 转动这个 all text inputs border 1px solid green 进
  • 是否可以选择以假的方式列出目标(可能带有描述)?

    在 Ruby RAKE 中 您可以通过以下方式记录您的任务 rakefile desc cleans temp task clean do p cleaning end desc compile the source task compil
  • 复制已过滤的数据子集:合并或事务复制?

    首先感谢您的阅读 我需要复制基于连接过滤器的数据子集 基于与其他表的联接的过滤器 Microsoft 使用联接过滤器 您可以将行过滤器从一个已发布的表扩展到另一个 这是设置 SQL Server 2012 事务复制订阅上的复制源 复制需要是
  • SSRS 2014 数据库设置 - 错误“使用其他版本的 SQL Server 作为报表数据源...”不受支持

    我正在 Windows Server 2012 R2 服务器上设置新的 SQL Server 2014 Enterprise Reporting Services 实例 在 Reporting Services 配置管理器中 当我选择要在其
  • 使更新进度面板位于中心

    我对更新进度面板有疑问 我想在屏幕中间安装更新进度面板 谁能建议我 这样做的想法是什么 您可以使用 css 来做到这一点
  • Environment.Exit 和 Main 中的简单返回 2 之间的区别

    从应用程序外部来看 两者之间有什么区别吗 Environment Exit 2 and static int Main return 2 最明显的区别是您可以从代码中的任何位置调用Environment Exit 除此之外 如果还有其他前台
  • 在 C# 中打印表单/用户控件

    我的计划 包含一个带有几个文本框和一个按钮的表单 默认打印机 设置为Adobe PDF在我的电脑上 My Goal 想要在用户单击 打印 按钮时截取表单 用户控件的屏幕截图 然后屏幕截图将以 pdf 格式保存在桌面上 我的问题 我的代码有以