Search code examples
c#printinggdi+system.drawing

Printing drawn objects in C#


I have an application that draws object on a Panel using

private void Canvas_Paint(object sender, PaintEventArgs e)

The function draws a user defined amount of rectangles that represent molds of urethane. There are also strings that label the "mold" on the Panel. The Panel is scrollable and, like I said, has a variable (and potentially large) amount of rectangles within it.

I have been asked to provide a printing function for these rectangles. I am completely new to C#, in fact I have never used it before this program. Is there a way to take what I have drawn on the Panel and print them? I have seen some methods of drawing to a PrintDocument, but it looks like I would have to redraw them inside the PrintPage function. I would like to just print what I have already drawn.


Solution

  • The image on the panel should be accessible through the .Image property. Using a new graphics object, draw that image to the PrintDocument.

    Alternatively, you could hold your own reference to the image in the form as you draw it.

    However, due to the difference between printers and screens, you're probably going to want to re-render the whole thing anyway using different anti-aliasing settings, etc.