VB.NET Print Large Image on Multiple Pages

Viewed 57

i have a problem printing an large image over multiple Pages. I have scanned A4 Documents with 1-x Pages and they put together as one long image. I have to rescale Image to fit in width on A4 and to print with as many pages as on image are. There ist my code, can't achieve this, printing only first page. Any help woud be aprechiated. Thanks! (i'm a beginner...)

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
0 Answers
Related