Printing Problem!

M

mehdi

Hi,
Consider a printing scenario where I have to draw the entire page on a
827x1169 (.01 inch) size. Thereafter, the entire bitmap has to be
resized to fill a given Bounds rectangle (keeping the aspect ratio
fixed). To do so, I've just wrote the following code in the PrintPage
event:

Graphics g = e.Graphics;
Rectangle bounds = TheDestinationRectangle;
Rectangle rc = new Rectangle(0, 0, (Int32)(827 * g.DpiX / 100), (Int32)
(1169 * g.DpiY / 100));
using (Bitmap img = new Bitmap(rc.Width, rc.Height))
{
using (Graphics bmp = Graphics.FromImage(img))
{
bmp.DrawLine(Pens.Black, rc.X, rc.Top, rc.Right, rc.Bottom); //
Line 1
bmp.DrawLine(Pens.Black, rc.Right, rc.Top, rc.Left,
rc.Bottom); //Line 2

g.DrawImage(img, bounds, rc, GraphicsUnit.Pixel);
}
}

However, something is really wrong with the above code since both of
those lines (commented out as 1 & 2), are neither anti-aliased nor
smooth. But if I draw the lines this way, everything is perfect:

Graphics g = e.Graphics;
Rectangle rc = new Rectangle(0, 0, 827, 1169);
g.DrawLine(Pens.Black, rc.X, rc.Top, rc.Right, rc.Bottom); //Line 1
g.DrawLine(Pens.Black, rc.Right, rc.Top, rc.Left, rc.Bottom); //Line 2

What am I doing wrong?

Any help would be highly appreciated,

TIA,
Mehdi
 
P

Peter Duniho

[...]
However, something is really wrong with the above code since both of
those lines (commented out as 1 & 2), are neither anti-aliased nor
smooth. But if I draw the lines this way, everything is perfect:

Graphics g = e.Graphics;
Rectangle rc = new Rectangle(0, 0, 827, 1169);
g.DrawLine(Pens.Black, rc.X, rc.Top, rc.Right, rc.Bottom); //Line 1
g.DrawLine(Pens.Black, rc.Right, rc.Top, rc.Left, rc.Bottom); //Line 2

This has very little to do with printing and everything to do with the
difference between drawing to a bitmap first and drawing directly to an
output device (any output device, whether the screen or a printer).

When you draw into the bitmap, you necessarily limit the resolution of
your lines to the resolution of the bitmap. In this case, that appears to
be 100 dpi (by default the Graphics in the PrintPageEventArgs is already
100 dpi). 100 dpi isn't really all that high resolution, and is usually
much lower than whatever the printer will be printing at.

For example, I've got a couple of ink jet printers here, one is some ten
years old, and for both of them the _draft_ mode is 360 dpi, with puts
approximately 10 times as many pixels into the same area as 100 dpi does..

So, in the code you posted, you're limiting the resolution of the lines to
100 dpi, while when you draw directly to e.Graphics, you use whatever
resolution the printer is printing at (which is almost certainly
considerably higher than 100 dpi).

Thus, jaggies the first way, smooth lines the second.

Note: just because the resolution reported by e.Graphics is 100 dpi, that
doesn't mean that's the actual output resolution. That just happens to be
the coordinate resolution with which your drawing will occur. It's
similar to setting the resolution and pixel measurement when drawing to
the screen; you aren't limited by the resolution with which you specify
drawing coordinates...only the resolution of the output device matters
(unless, of course, you run your graphics primitives through a
lower-resolution bitmap first, as you're doing here).

The moral of the story: if you want the best results, draw directly to the
output device, rather than to a bitmap first. That allows the output
device to take full advantage of the basic graphics primitives you're
using to compose the output.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top