用于打印的缩放图像 [英] Scaling image for printing
问题描述
我正在使用以下代码从 PictureBox 打印图像.如果图像比打印页面大,则除了缩小图像外,其他一切都很好.有没有我缺少的方法来做到这一点?
I'm using the following code to print an image from a PictureBox. All works great except for scaling images down if they are bigger than the print page. Is there a method I'm missing to do this?
屏幕截图,纸张边界外的大图:
Screenshot, large image outside of the paper bounds:
http://a.yfrog.com/img46/63/problemsh.pnghttp://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
推荐答案
替换:
Dim sz As New SizeF(100 * img.Width / img.HorizontalResolution, 100 * img.Height / img.VerticalResolution)
用这样的东西来适应页面中的图像:
With something like this to fit the image in the page:
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,但我没有时间对其进行测试,如果您不了解我所做的工作,那么调试起来会困难得多.很确定你会从代码中看到我在这里尝试做的事情!问候.
You could use algebra to solve for the proper scalefac, but I don't have time to test it and if you didn't understand what I'd done that would be a lot harder for you to debug. Pretty sure you'll see what I'm trying to do here from the just the code alone! Regards.
这篇关于用于打印的缩放图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!