GDI+ Bitmap drawing vs GDI+ PrintDocument drawing

GDI+ Bitmap drawing vs GDI+ PrintDocument drawing

Post by amxhdmFsbG » Wed, 29 Jun 2005 07:56:02


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
 
 
 

GDI+ Bitmap drawing vs GDI+ PrintDocument drawing

Post by Bob Powell » Wed, 29 Jun 2005 16:08:16

To get WYSIWYG output you must use a real-world measurement system for your
graphics. Converting between scales when using pixels is possible but why
work that hard?

Points, which are 1/72nd of an inch, are a good system to use if you're
dealing with text but inches, millimeters and the device setting which is
1/300th of an inch are also popular.

Choosing and using one drawing standard that isn't pixels will definitely be
the best thing you can do.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.yqcomputer.com/

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.yqcomputer.com/

Answer those GDI+ questions with the GDI+ FAQ
http://www.yqcomputer.com/

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.