【发布时间】:2016-01-28 15:15:26
【问题描述】:
我正在使用 WinForms。我有两种形式。在 form1 我有一个图片框,在 form2 我有一个打印按钮。我正在尝试构建一个应用程序,该应用程序将使用 Form2 从 Form1 中的图片框中打印图像。
我首先尝试制作一个面板并测试图片是否可以在form1中打印,并且确实可以,但是问题是当将打印代码复制到form2时,代码不允许我访问form1中的图片框。
如何从 Form2 访问 Form1 中的图片框,以便无法打印图片框中的图像。
我在 this.pictureBox1 下收到错误行。
Form2
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
var bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
this.pictureBox1.DrawToBitmap(bmp, this.pictureBox1.ClientRectangle);
e.Graphics.DrawImage(bmp, 25, 25, 800, 1050);
}
【问题讨论】:
-
你在form2中。所以
this.pictureBox1指的是form 2中的图片框,你必须将pictureBox1设为public并在form2中使用 -
它不是重复的。
-
并在form2中创建form1的实例? @M.kazemAkhgary
-
不好的建议是让你的图片框
public static。但我认为你不应该担心这一点,因为你只有这两种形式。 -
我还不完全理解静态的工作原理。对此还是陌生的。您的解决方案正在运行。
标签: c# .net winforms printing picturebox