Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何仅打印windows窗体的tablelayoutpanel和标签?_C#_Project - Fatal编程技术网

C# 如何仅打印windows窗体的tablelayoutpanel和标签?

C# 如何仅打印windows窗体的tablelayoutpanel和标签?,c#,project,C#,Project,我正在使用windows窗体进行支票打印项目,其中一个要求是通过单击“打印”按钮打印支票接收凭证,但它将打印整个窗口窗体,而不是只打印以下图像的白色部分 我用于打印预览事件处理程序的代码是: Graphics myGraphics = this.CreateGraphics(); Size s = this.Size; memoryImage = new Bitmap(s.Width, s.Height, myGraphics);// G

我正在使用windows窗体进行支票打印项目,其中一个要求是通过单击“打印”按钮打印支票接收凭证,但它将打印整个窗口窗体,而不是只打印以下图像的白色部分

我用于打印预览事件处理程序的代码是:

 Graphics myGraphics = this.CreateGraphics();
         Size s = this.Size;
         memoryImage = new Bitmap(s.Width, s.Height, myGraphics);//
         Graphics memoryGraphics = Graphics.FromImage(memoryImage);
         memoryGraphics.CopyFromScreen(label9.Location.X, label9.Location.Y, 52, 9, s);
         printPreviewDialog1.Document = PrintDoc1;
         PrintDoc1.PrintPage += printDocument2_PrintPage;
         printPreviewDialog1.ShowDialog()

任何人都可以告诉我如何解决我的问题吗?

如果没有其余的代码,很难确定,但看起来您发布的代码是在创建表单本身的图像,而不是要打印的
TableLayoutPanel
。使用时,它引用包含代码的类的当前实例;这大概是您的
表单
,它不是您想要打印的内容(但解释了为什么它会显示整个内容)

相反,您只需创建
TableLayoutPanel
(使用its)的图像并打印即可。无需创建
图形
对象或指定要复制的屏幕位置的精确坐标。例如:

//Create a temporary image to draw into
//with the dimensions of your TableLayoutPanel
using (Bitmap printImage = new Bitmap(myTableLayoutPanel.Width, myTableLayoutPanel.Height))
   {
      //Draw the TableLayoutPanel control to the temporary bitmap image
      myTableLayoutPanel.DrawToBitmap(printImage, new Rectangle(0, 0, printImage.Width, printImage.Height));

      //(...your code continues here, except that now you
      // will print the temporary image you just created)
      printPreviewDialog1.Document = PrintDoc1;
      PrintDoc1.PrintPage += printDocument2_PrintPage;
      printPreviewDialog1.ShowDialog()
   }
从您发布的代码中,我无法判断您将要打印的图像传递到打印预览对话框的准确程度,但是您在使用
memoryImage
之前应该使用上面示例代码中的新
printImage


请注意,如果控件的
Visible
属性设置为
False
,则
DrawToBitmap
方法不会绘制子
TextBox
控件,并且控件将按相反顺序绘制。您必须确保它的外观适合您的应用程序,但通常情况下,这是最简单的方法。

我从标题中假设白色部分是一个
表格布局面板
,您唯一要打印的是,对吗?是的,您是对的。你能告诉我怎么做吗?