代码之家  ›  专栏  ›  技术社区  ›  madlan

缩放图像以便打印

  •  3
  • madlan  · 技术社区  · 14 年前

    我正在使用以下代码从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
    
    2 回复  |  直到 11 年前
        1
  •  5
  •   FastAl    14 年前

    替换:

    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,但我没有时间来测试它,如果您不理解我所做的工作,那么您将很难调试它。你一定会看到我在这里只想从代码中看到什么!当做。

        2
  •  2
  •   lordfrankoo    11 年前
    Dim img As Image = PictureEdit1.Image
    e.Graphics.DrawImage(img, 0, 0, 
                         e.PageBounds.Width, e.PageBounds.Height)
    

    你需要解决的就是