VB.NET在多页上打印大图像

VB.NET在多页上打印大图像,vb.net,image,printing,Vb.net,Image,Printing,我在多页打印大图像时遇到问题。我扫描了A4纸的1-x页文档,它们被放在一起形成一张长图像。我必须重新缩放图像,以适应A4的宽度,并打印尽可能多的页面的图像。这是我的代码,无法实现,只打印第一页。任何帮助都会受到惩罚。谢谢(我是初学者…) 导入System.Drawing.Printing 导入System.Drawing.Imaging 导入System.IO 公开课表格1 将Bmp设置为位图 dim pageNum作为Int32=0 私有子PrintDocument4u PrintPage(B

我在多页打印大图像时遇到问题。我扫描了A4纸的1-x页文档,它们被放在一起形成一张长图像。我必须重新缩放图像,以适应A4的宽度,并打印尽可能多的页面的图像。这是我的代码,无法实现,只打印第一页。任何帮助都会受到惩罚。谢谢(我是初学者…)

导入System.Drawing.Printing
导入System.Drawing.Imaging
导入System.IO
公开课表格1
将Bmp设置为位图
dim pageNum作为Int32=0
私有子PrintDocument4u PrintPage(ByVal sender作为对象,ByVal e作为PrintPageEventArgs)处理PrintDocument4.PrintPage
尺寸宽度比为Double=e.MarginBounds.Width/Bmp.Width
Dim maxpagenum As Int32=数学天花板(Bmp.Height/e.MarginBounds.Height*WidthRatio)
Debug.WriteLine(maxpagenum)
调试写入线(e.MarginBounds.Height)
作为新位图的尺寸块(CInt(e.MarginBounds.Width/WidthRatio),CInt(e.MarginBounds.Height/WidthRatio))
作为新矩形的Dim dest(0,0,CInt(e.MarginBounds.Width/WidthRatio),CInt(e.MarginBounds.Height/WidthRatio))
使用gr作为图形=图形。FromImage(件)
将源矩形变暗为新矩形(0,0,CInt(e.MarginBounds.Width/WidthRatio),CInt(e.MarginBounds.Height/WidthRatio))
对于i,整数=1到maxpagenum
source_rect.X=0
'复制图像的一部分。
gr.DrawImage(Bmp、目标、源、_
GraphicsUnit.Pixel)
'piece.Save(“Bmp”&i&“.jpg”,ImageFormat.Bmp)
e、 图形.DrawImage(工件,0,源矩形Y,CInt(工件宽度*宽度比),CInt(工件高度*宽度比))
pageNum+=1
source_rect.Y+=CInt(例如边缘边界高度/宽度比)
如果pageNum
Imports System.Drawing.Printing
Imports System.Drawing.Imaging
Imports System.IO

Public Class Form1
   Dim Bmp As Bitmap
   dim pageNum As Int32 = 0

   Private Sub PrintDocument4_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles PrintDocument4.PrintPage

       Dim WidthRatio As Double = e.MarginBounds.Width / Bmp.Width
       Dim maxpagenum As Int32 = Math.Ceiling(Bmp.Height / e.MarginBounds.Height * WidthRatio)
       Debug.WriteLine(maxpagenum)
       Debug.WriteLine(e.MarginBounds.Height)

       Dim piece As New Bitmap(CInt(e.MarginBounds.Width / WidthRatio), CInt(e.MarginBounds.Height / WidthRatio))
       Dim dest_rect As New Rectangle(0, 0, CInt(e.MarginBounds.Width / WidthRatio), CInt(e.MarginBounds.Height / WidthRatio))
       Using gr As Graphics = Graphics.FromImage(piece)
           Dim source_rect As New Rectangle(0, 0, CInt(e.MarginBounds.Width / WidthRatio), CInt(e.MarginBounds.Height / WidthRatio))
           For i As Integer = 1 To maxpagenum
               source_rect.X = 0

               ' Copy the piece of the image.
               gr.DrawImage(Bmp, dest_rect, source_rect, _
                   GraphicsUnit.Pixel)
               'piece.Save("Bmp" & i & ".jpg", ImageFormat.Bmp)
               e.Graphics.DrawImage(piece, 0, source_rect.Y, CInt(piece.Width * WidthRatio), CInt(piece.Height * WidthRatio))
               pageNum += 1
               source_rect.Y += CInt(e.MarginBounds.Height / WidthRatio)
               If pageNum < maxpagenum Then
                   e.HasMorePages = True
               Else
                   pageNum = 0
               End If
           Next
           
       End Using
   End Sub

   Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
       If ListBox1.SelectedItem IsNot Nothing Then

           Dim selItem As String = ListBox1.SelectedItem.ToString()
           Dim fs1 As System.IO.FileStream
           fs1 = New System.IO.FileStream(selItem, IO.FileMode.Open, IO.FileAccess.Read)
           Bmp = New Bitmap(System.Drawing.Image.FromStream(fs1))
           fs1.Close()
          
           PrintDocument4 = New PrintDocument

           With PrintDialog1

               .AllowCurrentPage = False
               .AllowPrintToFile = False
               .AllowSelection = False
               .AllowSomePages = False
               .Document = PrintDocument4

               .PrinterSettings.DefaultPageSettings.Margins.Top = 15
               .PrinterSettings.DefaultPageSettings.Margins.Bottom = 15
               .PrinterSettings.DefaultPageSettings.Margins.Left = 15
               .PrinterSettings.DefaultPageSettings.Margins.Right = 15

           End With

           If PrintDialog1.ShowDialog = DialogResult.OK Then

               PrintDocument4.PrinterSettings = PrintDialog1.PrinterSettings
               AddHandler PrintDocument4.PrintPage, AddressOf PrintDocument4_PrintPage
               PrintDocument4.Print()

           End If
       End If
   End Sub