Converting Print page Graphics to Bitmap C#
bitmap, c#, graphics
Solution
You should actually draw the page into the bitmap, and then use ev.Graphics to draw that bitmap on the page.
private void doc_PrintPage(object sender, PrintPageEventArgs ev)
{
var bitmap = new Bitmap((int)graphics.ClipBounds.Width,
(int)graphics.ClipBounds.Height);
using (var g = Graphics.FromImage(bitmap))
{
// Draw all the graphics using into g (into the bitmap)
g.DrawLine(Pens.Black, 0, 0, 100, 100);
}
// And maybe some control drawing if you want...?
this.label1.DrawToBitmap(bitmap, this.label1.Bounds);
ev.Graphics.DrawImage(bitmap, 0, 0);
}
Problem
I have an application where the user can print a document of selected items in the form of an invoice. Everything works well however on the `PrintPage` event of the PrintDocument I want to capture the document or he graphics, turn it into a bitmap so I can save to it a `.bmp` for later use / viewing. (Note: There are multiple pages in this document) I have it set up like this: ``` PrintDocument doc = new PrintDocument(); doc.PrintPage += new PrintPageEventHandler(doc_PrintPage); doc.Print(); ``` Then on the `PrintPage` event: ``` private void doc_PrintPage(object sender, PrintPageEventArgs ev) { // Use ev.Graphics to create the document // I create the document here // After I have drawn all the graphics I want to get it and turn it into a bitmap and save it. } ``` I have cut out all the `ev.Graphics` code just because it is a lot of lines. Is there a way to turn the Graphics into a Bitmap without changing any of the code that draws graphics onto the `PrintDocument`? Or do something similar to that, maybe copying the document and converting it into a bitmap?