محمد اشرف
محمد اشرف

Reputation: 59

I have a windows form which contains an image & some textboxes on that image

I have a windows form which contains an image & some textboxes on that image. I need to print contents after I fill those textboxes along with that image. I've used below code, but it only prints the image, not the values in textboxes.

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);
}

but i am still not able to manage the print size

Upvotes: 0

Views: 178

Answers (1)

havardhu
havardhu

Reputation: 3616

The following code creates a bitmap of panel1 and all of its contents, values etc, using the DrawToBitmap method.

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);
}

Try this and see if it does what you're after

Upvotes: 1

Related Questions