2

I am trying to print an image (from file) to a the printer using a PrintDocument.

I am re-sizing my image so that it is scaled to be full page on the printout when I print this the image is cropped slightly.

EDIT 2

I am using the margins to calculate the area to use:

With printSettings.DefaultPageSettings
    Dim areaWidth As Single = .Bounds.Width - .Margins.Left - .Margins.Right
    Dim areaHeight As Single = .Bounds.Height - .Margins.Top - .Margins.Bottom
End With

The bounds of the page are 1169x827 (A4) and with the margins it is 1137x795.

After resize my image size is 1092x682 and I am using the following code to draw it: e.Graphics.DrawImage(printBitmap, .Margins.Left, .Margins.Top)

The annoying thing is that when I print to the PrintPreviewDialog it is scaled perfectly but when I print the exact same code to the actual printer it does not fit.

EDIT 3

Full code can be found at this url Usage:

Dim clsPrint As New clsPrinting
    With clsPrint
        .Landscape = True
        .SetMinimumMargins()
        If .ShowPrintDialog Then
            .Documentname = "Some doc name"
            .Preview = False 'When True shows ok
            .PrintImage("filename of a png file")
        End If
    End With
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • Could you post the code doing the resizing? It could be you're actually cropping the image, not resizing it. – briddums Sep 26 '11 at 10:54
  • According to the [`Bounds` documentation](http://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.bounds.aspx), I see it says, "Use the Bounds property along with the Margins property to calculate the printing area for the page." Should you perhaps be subtracting the values from `printSettings.DefaultPageSettings.Margins`? – lsuarez Sep 26 '11 at 14:07
  • The likely problem is in the image resizing code. Just don't resize it, draw it with the Graphics.DrawImage(Image, Rectangle) overload. You'll get much better output quality as a bonus. – Hans Passant Sep 26 '11 at 18:05
  • @Hans - Thanks but I don't think this is to blame. I tried using `.DrawImage` and this had exactly the same results. Fine in Print Preview but not when actually printed. I Think this could be a bug in the printer driver? – Matt Wilko Sep 27 '11 at 11:11

4 Answers4

1

You must work with MarginBounds:

in C#:

e.Graphics.DrawImage(your_image, e.MarginBounds);

in C++/CLI:

e->Graphics->DrawImage(your_image, e->MarginBounds);

Note: If your image doesn't have the same aspect ratio you'll need to adjust. In this example width of the image exceeded the page width:

Dim adjustment As Double = img.Width / e.MarginBounds.Width
e.Graphics.DrawImage(img, New Rectangle(New Point(0, 0), New Point(img.Width / adjustment, img.Height / adjustment)))
braX
  • 11,506
  • 5
  • 20
  • 33
Reza Ebrahimi
  • 3,623
  • 2
  • 27
  • 41
1

Try using e.graphics.VisibleClipBounds for the printable page size in the PrintPage function. As Hans said, it's better not to resize your image before printing.

xpda
  • 15,585
  • 8
  • 51
  • 82
  • `e.Graphics.VisibleClipBounds` contains the same width and height as the `PrintableArea` but the origin is 0,0 instead of the `Margin.Left`, `Margin.Top` – Matt Wilko Sep 27 '11 at 11:13
0

Sounds like you are wanting to print a page with a full bleed which most personal printers are not capable of. As one of the comments above mentions, factor in the margins to re-size the image to the appropriate size.

brad.huffman
  • 1,281
  • 8
  • 12
0

I did not find a solution to this problem. I worked around it by using the printer margins when performing a print preview and ignoring the margins (start at 0,0 origin) when actually printing. I believe that this is possibly a bug in the printer driver? But I can't confirm.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143