locked
Transfer photo folders to printing to printperview RRS feed

  • Question

  • I want to transfer all the images inside the folder regardless of their number to the Brent preview

    1 image of the folder means one page for printperview
    2 photo of the folder means 2 pages in printperview

    3 image of the folder means 3 page in printperview

    Friday, October 27, 2017 7:43 PM

Answers

  • This shows how. It uses a datagridview from which you can multiple select. You can modify it as required for your exact needs.

    In the future please show what you have already done using description and images. Ask specific questions.

    Imports System.Drawing.Printing Public Class Form9 Private WithEvents PrintDocument1 As PrintDocument = New PrintDocument Private PrintPreviewDialog1 As New PrintPreviewDialog Private PageCount As Integer Private Sub PrintingPicture_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.DoubleBuffered = True With DataGridView1 .Rows.Clear() .Columns.Clear() Dim img As New DataGridViewImageColumn() img.Name = "Image" img.Width = 160 .Columns.Add(img) 'fill the dgv with the files in this folder Dim di As New IO.DirectoryInfo("c:\bitmaps\") Dim aryFi As IO.FileInfo() = di.GetFiles("*." & "jpg") Dim fi As IO.FileInfo For Each fi In aryFi Dim n As Integer = .Rows.Add() .Rows.Item(n).Cells(0).Value = fi.Name .Rows.Item(n).Cells(0).Tag = fi.FullName MakeDGVThumbnail(fi.FullName, DataGridView1, n, 0) Next End With End Sub Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage 'drawsImage on printer graphics maintains image ratio, 'fits largest pic size to border size and centers the other direction Dim img As Image = Image.FromFile(DataGridView1.SelectedCells(PageCount).Tag.ToString) Dim l, t, w, h As Integer Dim ratio As Single = CSng(img.Width / img.Height) If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then w = e.MarginBounds.Width h = CInt(w / ratio) t = CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - (h / 2)) l = e.MarginBounds.Left Else h = e.MarginBounds.Height w = CInt(h * ratio) l = CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (w / 2)) t = e.MarginBounds.Top End If 'draw the image on the printer graphics zoom to margins e.Graphics.DrawImage(img, l, t, w, h) img.Dispose() PageCount += 1 If PageCount >= DataGridView1.SelectedCells.Count Then e.HasMorePages = False PageCount = 0 Else e.HasMorePages = True End If End Sub Private Sub MakeDGVThumbnail(theFilenamePath As String, dgv As DataGridView, dgvIndex As Integer, dgvColumn As Integer) 'draw a thumbnail for dgv Dim ratio As Single Dim thisBmp As Bitmap = New Bitmap(theFilenamePath) Dim w As Integer = thisBmp.Width Dim h As Integer = thisBmp.Height ratio = CSng(h / w) w = dgv.Columns(dgvColumn).Width If ratio > 1 Then h = 100 w = CInt(h / ratio) Else h = CInt(w * ratio) End If Dim border As Integer = 3 w -= (2 * border) If w < 2 Then w = 2 h -= (2 * border) If h < 2 Then h = 2 Using bmp As New Bitmap(w, h) Using g As Graphics = Graphics.FromImage(bmp) g.DrawImage(thisBmp, border, border, bmp.Width, bmp.Height) dgv.Rows.Item(dgvIndex).Height = h + (2 * border) dgv.Rows.Item(dgvIndex).Cells(dgvColumn).Value = bmp.Clone End Using End Using

    thisBmp.Dispose() End Sub Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click Try 'call the print dialog 'click the print button on the dialog to print PageCount = 0 PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() Catch ex As Exception MsgBox("Printing Problem" & Chr(13) & ex.Message, MsgBoxStyle.Exclamation) End Try End Sub End Class



    PS LOL Hi Razerz!
    • Edited by tommytwotrain Friday, October 27, 2017 10:43 PM
    • Marked as answer by ahmeddc Saturday, October 28, 2017 12:36 AM
    Friday, October 27, 2017 10:28 PM
  • That is because you are trying to add an array of FileInfo to a List that is expecting an array of Strings.  You can just do this....

        Private Sub Button_Print_Click(sender As Object, e As EventArgs) Handles Button_Print.Click
            ImageFileNames.Clear()
            ImageFileNames.AddRange(IO.Directory.GetFiles("c:\image\a4", "*.png"))
            PrintPreviewDialog1.ShowDialog()
        End Sub


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, October 28, 2017 12:31 AM
    • Marked as answer by ahmeddc Saturday, October 28, 2017 12:36 AM
    Saturday, October 28, 2017 12:30 AM

All replies

  • Do you mean Print Preview?   Windows Explorer generates thumbnails for print preview automatically, so you must be referring to some other print preview facility. Is it something like this:
    https://www.gammon.com.au/quickslideshow/thumbnails.htm
    Friday, October 27, 2017 8:30 PM
  • no with vb.net code 
    Friday, October 27, 2017 8:57 PM
  •  Are you using a PrintPreviewDialog or are you using a PrintPreviewControl ?

    If you say it can`t be done then i`ll try it

    Friday, October 27, 2017 9:10 PM
  • Are you using a PrintPreviewDialog  

    Yes I use 



    • Edited by ahmeddc Friday, October 27, 2017 9:17 PM
    Friday, October 27, 2017 9:16 PM
  • Yes I use 

    What is the problem with using the print preview dialog?

    Friday, October 27, 2017 9:45 PM
  • I want to transfer all the images inside the folder regardless of their number to the Brent preview

    1 image of the folder means one page for printperview
    2 photo of the folder means 2 pages in printperview

    3 image of the folder means 3 page in printperview

    with vb.net code 

    Friday, October 27, 2017 9:53 PM
  • This shows how. It uses a datagridview from which you can multiple select. You can modify it as required for your exact needs.

    In the future please show what you have already done using description and images. Ask specific questions.

    Imports System.Drawing.Printing Public Class Form9 Private WithEvents PrintDocument1 As PrintDocument = New PrintDocument Private PrintPreviewDialog1 As New PrintPreviewDialog Private PageCount As Integer Private Sub PrintingPicture_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.DoubleBuffered = True With DataGridView1 .Rows.Clear() .Columns.Clear() Dim img As New DataGridViewImageColumn() img.Name = "Image" img.Width = 160 .Columns.Add(img) 'fill the dgv with the files in this folder Dim di As New IO.DirectoryInfo("c:\bitmaps\") Dim aryFi As IO.FileInfo() = di.GetFiles("*." & "jpg") Dim fi As IO.FileInfo For Each fi In aryFi Dim n As Integer = .Rows.Add() .Rows.Item(n).Cells(0).Value = fi.Name .Rows.Item(n).Cells(0).Tag = fi.FullName MakeDGVThumbnail(fi.FullName, DataGridView1, n, 0) Next End With End Sub Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage 'drawsImage on printer graphics maintains image ratio, 'fits largest pic size to border size and centers the other direction Dim img As Image = Image.FromFile(DataGridView1.SelectedCells(PageCount).Tag.ToString) Dim l, t, w, h As Integer Dim ratio As Single = CSng(img.Width / img.Height) If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then w = e.MarginBounds.Width h = CInt(w / ratio) t = CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - (h / 2)) l = e.MarginBounds.Left Else h = e.MarginBounds.Height w = CInt(h * ratio) l = CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (w / 2)) t = e.MarginBounds.Top End If 'draw the image on the printer graphics zoom to margins e.Graphics.DrawImage(img, l, t, w, h) img.Dispose() PageCount += 1 If PageCount >= DataGridView1.SelectedCells.Count Then e.HasMorePages = False PageCount = 0 Else e.HasMorePages = True End If End Sub Private Sub MakeDGVThumbnail(theFilenamePath As String, dgv As DataGridView, dgvIndex As Integer, dgvColumn As Integer) 'draw a thumbnail for dgv Dim ratio As Single Dim thisBmp As Bitmap = New Bitmap(theFilenamePath) Dim w As Integer = thisBmp.Width Dim h As Integer = thisBmp.Height ratio = CSng(h / w) w = dgv.Columns(dgvColumn).Width If ratio > 1 Then h = 100 w = CInt(h / ratio) Else h = CInt(w * ratio) End If Dim border As Integer = 3 w -= (2 * border) If w < 2 Then w = 2 h -= (2 * border) If h < 2 Then h = 2 Using bmp As New Bitmap(w, h) Using g As Graphics = Graphics.FromImage(bmp) g.DrawImage(thisBmp, border, border, bmp.Width, bmp.Height) dgv.Rows.Item(dgvIndex).Height = h + (2 * border) dgv.Rows.Item(dgvIndex).Cells(dgvColumn).Value = bmp.Clone End Using End Using

    thisBmp.Dispose() End Sub Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click Try 'call the print dialog 'click the print button on the dialog to print PageCount = 0 PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() Catch ex As Exception MsgBox("Printing Problem" & Chr(13) & ex.Message, MsgBoxStyle.Exclamation) End Try End Sub End Class



    PS LOL Hi Razerz!
    • Edited by tommytwotrain Friday, October 27, 2017 10:43 PM
    • Marked as answer by ahmeddc Saturday, October 28, 2017 12:36 AM
    Friday, October 27, 2017 10:28 PM
  •  Maybe this is what you want to do?  After each image is drawn,  set the HasMorePages property to True.  The example below will let you select multiple images and they will each be printed on their own page.  If you select 4 images,  they will be printed to 4 separate pages.

     I only drew the images as they where,  you can do the scaling of the images if you want.  That is not the point of the example,  the point is to draw one image,  and set the HasMorePages property to True if there is another image.  Keep setting it to True until the last image has been printed.

    Imports System.Drawing.Printing
    
    Public Class Form1
        Private ImageFileNames As New List(Of String)
        Private Indx As Integer = 0
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With PrintDocument1.DefaultPageSettings
                .PaperSize = (From s As PaperSize In PrintDocument1.PrinterSettings.PaperSizes.Cast(Of PaperSize) Where s.RawKind = PaperKind.A4).FirstOrDefault 'sets the paper size to A4
                .Margins = New Margins(10, 10, 30, 30) 'sets the left, right, top, bottom margins for the printed page
                .PrinterSettings.PrinterName = "Microsoft XPS Document Writer" 'set the name of the printer
                .PrinterSettings.PrintFileName = "C:\TestFolder\Printed Images\PrintedImages.xps" 'set this to the output filename
                .PrinterSettings.PrintToFile = True 'tell it to print to a file
            End With
            PrintPreviewDialog1.Document = PrintDocument1
        End Sub
    
        Private Sub Button_OpenImages_Click(sender As Object, e As EventArgs) Handles Button_OpenImages.Click
            Using ofd As New OpenFileDialog
                ofd.Filter = "Images|*.png;*.jpg;*.bmp"
                ofd.Multiselect = True
                If ofd.ShowDialog = DialogResult.OK Then
                    ImageFileNames.Clear()
                    ImageFileNames.AddRange(ofd.FileNames)
                    Me.Text = ImageFileNames.Count.ToString & " Images To Print..."
                End If
            End Using
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button_Print.Click
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
        Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
            Indx = 0
        End Sub
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Using bm As New Bitmap(ImageFileNames(Indx))
                e.Graphics.DrawImage(bm, e.MarginBounds.Left, e.MarginBounds.Top, bm.Width, bm.Height)
            End Using
            Indx += 1
            e.HasMorePages = (Indx < ImageFileNames.Count)
        End Sub
    End Class
     

     Here i selected 4 images to print,  you can see they are all on their own page.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Friday, October 27, 2017 10:31 PM
    Friday, October 27, 2017 10:29 PM
  • hi    ironrazerz  

    There is a problem for not showing the whole picture and showing part of it note that the image to be transferred a4 or pos printer width of 8 cm

    I want to deal with Silent folde

     "c\image"

    image show

    https://www.dropbox.com/s/wa6xmq1859cew3t/print%20image.png?dl=0

    Unsupported images show for my account 

    Friday, October 27, 2017 10:59 PM
  • hi    ironrazerz  

    There is a problem for not showing the whole picture and showing part of it note that the image to be transferred a4 or pos printer width of 8 cm

    I want to deal with Silent folde

     "c\image"

    image show

    https://www.dropbox.com/s/wa6xmq1859cew3t/print%20image.png?dl=0

    Unsupported images show for my account 

     That is why i said that you need to do the scaling of the drawn images.  I only showed a basic example of how to put each image on a separate page which is what i believe you asked.

     I also have no idea what "Unsupported images show for my account" means.  You are not making it clear enough in the english language as to what you are asking or saying.


    If you say it can`t be done then i`ll try it

    Friday, October 27, 2017 11:25 PM
  •  Try changing the PrintPage event sub code like this...

        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Using bm As New Bitmap(ImageFileNames(Indx))
                Dim l, t, w, h As Integer
                Dim ratio As Single = CSng(bm.Width / bm.Height)
    
                If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then
                    w = e.MarginBounds.Width
                    h = CInt(w / ratio)
                    t = CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - (h / 2))
                    l = e.MarginBounds.Left
                Else
                    h = e.MarginBounds.Height
                    w = CInt(h * ratio)
                    l = CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (w / 2))
                    t = e.MarginBounds.Top
                End If
    
                e.Graphics.DrawImage(bm, l, t, w, h)
            End Using
            Indx += 1
            e.HasMorePages = (Indx < ImageFileNames.Count)
        End Sub
    


    If you say it can`t be done then i`ll try it

    Friday, October 27, 2017 11:30 PM
  • I modified the code and it works well with me. I want to deal silently with the folder

    I used this code but there was a problem

    folder

    "c\imag"

    Saturday, October 28, 2017 12:04 AM
  • I used this code but there was a problem

    folder

    "c\imag"

     What problem,  what does it do or not do?  Are you getting an error or what?  We can not see your code from our houses,  you need to post your code here and explain the problem if you want help.

    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, October 28, 2017 12:14 AM
    Saturday, October 28, 2017 12:13 AM
  • I used this code but there was a problem

    To place the folder containing the images "c:\image\a4"

    Dim di As New IO.DirectoryInfo("c:\image\a4") Dim aryFi As IO.FileInfo() = di.GetFiles("*." & "png") ImageFileNames.AddRange(aryFi)


    Saturday, October 28, 2017 12:19 AM
  • That is because you are trying to add an array of FileInfo to a List that is expecting an array of Strings.  You can just do this....

        Private Sub Button_Print_Click(sender As Object, e As EventArgs) Handles Button_Print.Click
            ImageFileNames.Clear()
            ImageFileNames.AddRange(IO.Directory.GetFiles("c:\image\a4", "*.png"))
            PrintPreviewDialog1.ShowDialog()
        End Sub


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, October 28, 2017 12:31 AM
    • Marked as answer by ahmeddc Saturday, October 28, 2017 12:36 AM
    Saturday, October 28, 2017 12:30 AM
  • hiIronRazerz Thank you very much for doing more than wonderful I told you my greetings good luck
    Saturday, October 28, 2017 12:36 AM
  • hiIronRazerz Thank you very much for doing more than wonderful I told you my greetings good luck

     You're Welcome.  8)

    If you say it can`t be done then i`ll try it

    Saturday, October 28, 2017 1:27 AM
  • hiIronRazerz Thank you very much for doing more than wonderful I told you my greetings good luck


     You're Welcome.  8)

    If you say it can`t be done then i`ll try it

    This is the nobility of your morals
    Saturday, October 28, 2017 1:38 AM