G
Guest
Hi I'm trying to get WYSIWYG print output by exposing the painting method of
a custom control I've written and everything is just a bit off.
Here's what I've done. I have a method on my custom control (a Panel that I
paint on) that accepts a Graphics object. This method draws text, lines,
etc. on the Graphics object. The method is public so I can pass the Graphics
object of a PrintDocument to it for printing purposes or use the method
internally to render my control on a Form.
When the control is rendered on a Form I create a Bitmap, do
GraphicsFromBitmap to get a Graphics object and then pass it to my paint
method to be drawn on. The returned painted bitmap can be resized to fit the
control and scaled and AutoScrolled to give a zoom feature. So far so good.
When instead I want to print the content of my control, I create an instance
of a class that extends PrintDocument and in my PrintPage override I get the
Graphics from the passed PrintPageEventArgs parameter and pass it to my
control's publicly exposed print method to be drawn on.
The output looks great save for one thing. The dimensions of some of my
drawn elements are slightly different causing some textual elements to be
wrapped at different spots or not appear on the correct page (I calculate
overflowed boundaries using MeasureCharacterRanges).
So since the paint method is the same in both cases the only difference is
the passed Graphics object that is being drawn on. I notice one difference -
the DpiX and DpiY for the two Graphics objects are different (96,96 - for the
bitmap and 600,600 - for the PrintDocument). Playing with the
SetResolution() method on Bitmap seems to be a possibility but here is what I
really want to know:
What properties of the respective Graphics objects should I tweak to ensure
my PrintDocument Graphics content matches my Bitmap Graphics content?
Am I way off in my approach and need to understand something fundamental
about GDI+ and or printing?
Any other advice or offer to point me to a tutorial or other resource would
also be appreciated.
Jack
a custom control I've written and everything is just a bit off.
Here's what I've done. I have a method on my custom control (a Panel that I
paint on) that accepts a Graphics object. This method draws text, lines,
etc. on the Graphics object. The method is public so I can pass the Graphics
object of a PrintDocument to it for printing purposes or use the method
internally to render my control on a Form.
When the control is rendered on a Form I create a Bitmap, do
GraphicsFromBitmap to get a Graphics object and then pass it to my paint
method to be drawn on. The returned painted bitmap can be resized to fit the
control and scaled and AutoScrolled to give a zoom feature. So far so good.
When instead I want to print the content of my control, I create an instance
of a class that extends PrintDocument and in my PrintPage override I get the
Graphics from the passed PrintPageEventArgs parameter and pass it to my
control's publicly exposed print method to be drawn on.
The output looks great save for one thing. The dimensions of some of my
drawn elements are slightly different causing some textual elements to be
wrapped at different spots or not appear on the correct page (I calculate
overflowed boundaries using MeasureCharacterRanges).
So since the paint method is the same in both cases the only difference is
the passed Graphics object that is being drawn on. I notice one difference -
the DpiX and DpiY for the two Graphics objects are different (96,96 - for the
bitmap and 600,600 - for the PrintDocument). Playing with the
SetResolution() method on Bitmap seems to be a possibility but here is what I
really want to know:
What properties of the respective Graphics objects should I tweak to ensure
my PrintDocument Graphics content matches my Bitmap Graphics content?
Am I way off in my approach and need to understand something fundamental
about GDI+ and or printing?
Any other advice or offer to point me to a tutorial or other resource would
also be appreciated.
Jack