我正在使用以下代码从 PictureBox 打印图像。
除了当图像大于打印页面时缩小图像之外,一切都很好。
我缺少一种方法来做到这一点吗?
屏幕截图,超出纸张范围的大图像:
http://a.yfrog.com/img46/63/problemsh.png 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
Replace:
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)
您可以使用代数来求解正确的比例因子,但我没有时间测试它,如果您不明白我做了什么,那么调试起来就会困难得多。很确定您仅通过代码就能明白我在这里试图做什么!问候。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)