void pdoc_PrintControl(object sender, PrintPageEventArgs e) { PrintDocumentControl pdc = sender as PrintDocumentControl; if (pdc != null) { if (pdc.ControlToPrint != null) { Bitmap bmp = CaptureWindowImage(pdc.ControlToPrint.Handle); if (bmp != null) { e.Graphics.DrawImage(bmp, 0, 0); } } } }
public void PrintControl(Control control, string documentName, bool preview) { if (control != null && !control.Disposing && !control.IsDisposed && control.IsHandleCreated) { PrintDocumentControl pdoc = new PrintDocumentControl(); pdoc.ControlToPrint = control; pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintControl); pdoc.DocumentName = documentName; if (preview) { PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.Document = pdoc; dlg.ShowDialog(control.FindForm()); } else { pdoc.Print(); } } }