如何使用VB.NET调整图像尺寸以适应打印需求?

2026-05-08 12:483阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计480个文字,预计阅读时间需要2分钟。

如何使用VB.NET调整图像尺寸以适应打印需求?

我正在使用以下代码从PictureBox打印图像。除了缩小图像(如果它们大于打印页面)以外,所有这些都很有效。我有什么方法可以实现这一点吗?截图,纸张边缘外的图像:[链接](http://a.yfrog.com/img)

我正在使用以下代码从PictureBox打印图像.
除了缩小图像(如果它们大于打印页面)以外,所有这些都很有效.
我有什么方法可以做到这一点吗?

截图,纸张边界外的大图:

a.yfrog.com/img46/63/problemsh.png 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)

使用类似的东西来适应页面中的图像:

如何使用VB.NET调整图像尺寸以适应打印需求?

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

本文共计480个文字,预计阅读时间需要2分钟。

如何使用VB.NET调整图像尺寸以适应打印需求?

我正在使用以下代码从PictureBox打印图像。除了缩小图像(如果它们大于打印页面)以外,所有这些都很有效。我有什么方法可以实现这一点吗?截图,纸张边缘外的图像:[链接](http://a.yfrog.com/img)

我正在使用以下代码从PictureBox打印图像.
除了缩小图像(如果它们大于打印页面)以外,所有这些都很有效.
我有什么方法可以做到这一点吗?

截图,纸张边界外的大图:

a.yfrog.com/img46/63/problemsh.png 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)

使用类似的东西来适应页面中的图像:

如何使用VB.NET调整图像尺寸以适应打印需求?

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