2010-08-15 49 views
3

我使用下面的代碼從PictureBox打印圖像。 所有的作品都很棒,除了縮小圖像大小比打印頁面大。 有沒有一種方法我錯過了這樣做?縮放打印圖像

截圖,紙張邊界外大的圖像:

http://a.yfrog.com/img46/63/problemsh.png http://a.yfrog.com/img46/63/problemsh.png

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    AddHandler PrintDocument1.PrintPage, AddressOf OnPrintPage 

    With PageSetupDialog1 
     .Document = PrintDocument1 
     .PageSettings = PrintDocument1.DefaultPageSettings 

     If PictureEdit1.Image.Height >= PictureEdit1.Image.Width Then 
      PageSetupDialog1.PageSettings.Landscape = False 
     Else 
      PageSetupDialog1.PageSettings.Landscape = True 
     End If 

    End With 

    PrintDialog1.UseEXDialog = True 
    PrintDialog1.Document = PrintDocument1 

    If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then 
     PrintPreviewDialog1.Document = PrintDocument1 
     If PrintPreviewDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then 

      PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings 
      PrintDocument1.Print() 

     End If 
    End If 
End Sub 

Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) 
    Dim img As Image = PictureEdit1.Image 

    Dim sz As New SizeF(100 * img.Width/img.HorizontalResolution, 100 * img.Height/img.VerticalResolution) 
    Dim p As New PointF((e.PageBounds.Width - sz.Width)/2, (e.PageBounds.Height - sz.Height)/2) 
    e.Graphics.DrawImage(img, p) 
End Sub 

回答

5

替換:

Dim sz As New SizeF(100 * img.Width/img.HorizontalResolution, 100 * img.Height/img.VerticalResolution) 

像這樣的東西,以適應圖像中的頁面:

dim ScaleFac as integer = 100 
While (ScaleFac * img.Width/img.HorizontalResolution > e.PageBounds.Width or ScaleFac * img.Height/img.VerticalResolution > e.PageBounds.Height) and ScaleFac > 2 
    ScaleFac -= 1 
Wend 
Dim sz As New SizeF(ScaleFac * img.Width/img.HorizontalResolution, ScaleFac* img.Height/img.VerticalResolution) 

您可以使用代數來解決正確的scalefac問題,但我沒有時間來測試它,如果您不明白我做了什麼,那麼您將很難進行調試。很確定你會看到我在這裏僅僅從代碼中看到了什麼!問候。

+0

的偉大工程,使培訓就業處是 - Scalefac :) – madlan 2010-08-19 20:26:14

2
Dim img As Image = PictureEdit1.Image 
e.Graphics.DrawImage(img, 0, 0, 
        e.PageBounds.Width, e.PageBounds.Height) 

是一切,你將需要解決

+0

這將圖像調整到頁面大小,但將拓展圖像兩種方式。它不保持相同的寬高比,因此會扭曲圖像。在大多數情況下,這是不希望的。 – 2015-10-22 07:40:52