gpt4 book ai didi

c# - 在 C# 中打印 Form/UserControl

转载 作者:太空狗 更新时间:2023-10-29 21:50:30 25 4
gpt4 key购买 nike

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

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

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

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

问题一:程序截取的截图在打印时与页面不符。 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# 语言,非常感谢您的帮助。 :)

最佳答案

好的,检查一下,特别是修改后的 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;
}
}

关于c# - 在 C# 中打印 Form/UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19165294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com