-1

I have a windows form that contains a table, two labels above it and many other components underneath By using the PrintDocument I would like to print the table with its title, and only that without showing the other components. This is my form

The code I use prints the table only.

Bitmap printImage;
private void CaptureScreen()
{ 
    printImage = new Bitmap(tableLayoutPanel1.Width, tableLayoutPanel1.Height);
    tableLayoutPanel1.DrawToBitmap(printImage, new Rectangle(0, 0, printImage.Width, printImage.Height));
}

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    e.Graphics.DrawImage(printImage, 0, 0);
}

How do I combine the label?

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Sujood
  • 15
  • 5

1 Answers1

0

In your print code, use Graphics.MeasureString to get the size of your label text (or use the label height), use that to offset your bitmap (instead of 0, 0), and then use the Graphics object to draw the string using the Text, Forecolor, and Font properties of the label. If you want to use the label backcolor, do a Graphics.FillRectangle prior to drawing the string.

You may need to get creative if your label text is auto-wrapped and you want it to print that way.

Sean K
  • 774
  • 7
  • 10