How to print graphic object via PrintDocument object?

  • Thread starter Thread starter Krich
  • Start date Start date
K

Krich

I am still new on printdocument object. I have a problem
try to print graphic object. I have one graphics object.
How to print it? I try to print it in PrintPage event
using this syntax
e.Graphics = grhObj
by grhObj is graphics object that already has string, line
or other image in it. It is very difficult to recreate
this object in PrintPage event.
 
Krich,
It is very difficult to recreate
this object in PrintPage event.
IMHO its not as difficult as you may think!

Think of the Graphics object as a write once object. Unless you use
Graphics.FromImage to draw on a bitmap, what you draw is lost.

Also the resolution of most printers is far greater then your screen, if you
physically printed the bits from the screen you would loose resolution.

The code you use to draw on the screen should be the exact same code you use
to print on a page.

What I normally do is have the Control.Paint event call the same subroutine
that the PrintDocument.PrintPage event calls.

Private Sub Control_Paint(...) Handles ...Paint
Initialize(e.Graphics)
DrawObject(e.Graphics)
End Sub

Private Sub PrintDocument_PrintPage(...) Handles ...PrintPage
Initialize(e.Graphics)
DrawObject(e.Graphics)
End Sub

NOTE: Most of the initialization above is shared such as PageUnit,
PageScale, & zoom factor, so I call a common routine... I may need to
initialize a Graphics object without actually drawing so this is a
standalone routine.

Private Sub Initialize(ByVal gr As Graphics)
gr.PageUnit = GraphicsUnit.Millimeter
gr.PageScale = 1 ' My project uses Millimeters for the drawing
units.
gr.ScaleTransform(m_zoom, m_zoom)
End Sub

Then the DrawObject method will have all the logic to draw the object

Private Sub DrawObject(ByVal gr As Graphics)
gr.DrawString(...)
gr.DrawLine(...)
gr.DrawImage(...)
End Sub

Above DrawObject sub routine may actually be a method of a class, rather
then a member of the form itself.

Hope this helps
Jay
 
Back
Top