开发者

How to print only tablelayoutpanel and label of windows form?

开发者 https://www.devze.com 2023-01-25 17:06 出处:网络
I\'m working chequePrinting project using windows-form, where one of my requirement is to printCheque Receiving voucher by clicking print button, but it\'s giving me the print out of whole window form

I'm working chequePrinting project using windows-form, where one of my requirement is to print Cheque Receiving voucher by clicking print button, but it's giving me the print out of whole window form instead of giving only white part of the following image.

How to print only tablelayoutpanel and label of windows form?

The code, which I'm using for prin开发者_开发百科t preview event handler is:

 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()

Could anyone please tell how can I solve my problem?


It's hard to tell for sure without the rest of the code, but it looks like the code that you posted is creating an image of the form itself, rather than the TableLayoutPanel that you want to print. When you use the this keyword, it refers to the current instance of the class containing your code; presumably this is your Form, which is not what you want to print (but explains why it's showing the whole thing).

Instead, you can simply create an image of the TableLayoutPanel (using its DrawToBitmap method) and print that. There's no need to create a Graphics object or specify the exact coordinates of the screen location that you wish to copy. For example:

//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()
   }

I can't tell from the code that you posted how exactly you're passing the image that is to be printed to your print preview dialog, but however you got it working with memoryImage before should work with the new printImage in the sample code above.

Note that the DrawToBitmap method will not draw a child TextBox control if its Visible property is set to False, and that your controls will be drawn in reverse order. You'll have to make sure that it's appearance is acceptable for your application, but more often than not, this is the easiest way.

0

精彩评论

暂无评论...
验证码 换一张
取 消