gpt4 book ai didi

vb.net - 缩放图像以进行打印

转载 作者:行者123 更新时间:2023-12-01 10:13:30 25 4
gpt4 key购买 nike

我正在使用以下代码从 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

最佳答案

替换:

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,但我没有时间对其进行测试,如果您不理解我所做的事情,那么您将很难进行调试。很确定你会从代码中看到我在这里尝试做的事情!问候。

关于vb.net - 缩放图像以进行打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3489010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com