开发者

Issues Resizing Image to fit on a printed page

开发者 https://www.devze.com 2023-04-07 06:28 出处:网络
I am trying to print an image (from fil开发者_StackOverflow中文版e) to a the printer using a PrintDocument.

I am trying to print an image (from fil开发者_StackOverflow中文版e) 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


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.


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)))


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号