0

我有一个 Windows 窗体,其中包含图像和该图像上的一些文本框。在将这些文本框与该图像一起填充后,我需要打印内容。我使用了下面的代码,但它只打印图像,而不是文本框中的值。

PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += PrintDocumentOnPrintPage;
printDocument.PrinterSettings.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("a2", this.Width, this.Height);
PrintDialog pdg = new PrintDialog();
pdg.Document = printDocument;
// if (pdg.ShowDialog() == DialogResult.OK)
//{
     printDocument.Print();
//}
private void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e)
{
     Graphics myGraphics = panel1.CreateGraphics();

     Size s = this.Size;
     Bitmap memoryImage = new Bitmap(panel1.Width , panel1.Height, myGraphics);
     Graphics memoryGraphics = Graphics.FromImage(memoryImage);
     Point screenLoc =PointToScreen(panel1.Location); // Get the location of the Panel in Screen Coordinates
     memoryGraphics.CopyFromScreen(screenLoc.X, screenLoc.Y, 0, 0, s);

     e.Graphics.DrawImage(memoryImage, 0, 0);
}

但我仍然无法管理打印尺寸

4

1 回答 1

1

DrawToBitmap以下代码使用该方法创建 panel1 及其所有内容、值等的位图。

private void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e)
{
    Bitmap image = new Bitmap(panel1.Width, panel1.Height);
    this.panel1.DrawToBitmap(image, panel1.Bounds);
    e.Graphics.DrawImage(image, 0, 0);
}

试试这个,看看它是否符合你的要求

于 2013-06-03T09:31:14.487 回答