Printing Problem!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mehdi

    #1

    Printing Problem!

    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 = TheDestinationR ectangle;
    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.FromIm age(img))
    {
    bmp.DrawLine(Pe ns.Black, rc.X, rc.Top, rc.Right, rc.Bottom); //
    Line 1
    bmp.DrawLine(Pe ns.Black, rc.Right, rc.Top, rc.Left,
    rc.Bottom); //Line 2

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

    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

  • Peter Duniho

    #2
    Re: Printing Problem!

    On Mon, 16 Jul 2007 04:07:49 -0700, mehdi <mehdi.mousavi@ gmail.comwrote:
    [...]
    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 PrintPageEventA rgs 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...o nly 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

    Comment

    Working...