Results 1 to 14 of 14

Thread: How Can I Print Images from DataGridView ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    How Can I Print Images from DataGridView ?

    Hi everyone.

    I listed all records into DataGridView (DGV). and in some records has image beside other data and some has not image.

    When I filtered records... some rows has image some rows not.

    When I try to print each record data per PrintDocument page, then I get only empty images.

    for example. first record (DGV first Row) has image and second row has not image.
    if I select only first row I get image but if I slect first and second rows. then I get both without any image.

    here is my code...

    Code:
     Dim img() As Byte
    
            For i As Integer = 0 To DGV.RowCount - 1
    
                If DGV.CurrentRow.Cells(17).Value IsNot DBNull.Value Then
    
                    img = CType(DGV.Rows(i).Cells(17).Value, Byte()) 'TableSql(2)(4) (CType(BndngSrcRprt(Sayac)("Resim"), Byte()))
    
                    Dim Ms As New MemoryStream(img)
    
                    e.Graphics.DrawImage(Image.FromStream(Ms), 150, 20, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
                Else
    
                    e.Graphics.DrawRectangle(Pens.Black, 150, 20, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
    
                End If
            Next

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How Can I Print Images from DataGridView ?

    Change this…

    Code:
    If DGV.CurrentRow.Cells(17).Value IsNot DBNull.Value Then
    To this…

    Code:
    If DGV.Rows(I).Cells(17).Value IsNot DBNull.Value Then

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    Re: How Can I Print Images from DataGridView ?

    Quote Originally Posted by .paul. View Post
    Change this…

    Code:
    If DGV.CurrentRow.Cells(17).Value IsNot DBNull.Value Then
    To this…

    Code:
    If DGV.Rows(I).Cells(17).Value IsNot DBNull.Value Then

    thanks but this time put first row image to all rows...

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How Can I Print Images from DataGridView ?

    Code:
    For i As Integer = 0 To DGV.RowCount - 1
    
        Dim imgBytes() As Byte = TryCast(DGV.Rows(i).Cells(17).Value, Byte())
    
        If Not imgBytes Is Nothing Then
    
            Dim Ms As New MemoryStream(imgBytes)
    
            e.Graphics.DrawImage(Image.FromStream(Ms), 150, 20, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
        Else
    
            e.Graphics.DrawRectangle(Pens.Black, 150, 20, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
    
        End If
    Next

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How Can I Print Images from DataGridView ?

    Don't forget you have hardcoded x, y values there. Your images will all be drawn in the same place...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    Re: How Can I Print Images from DataGridView ?

    Quote Originally Posted by .paul. View Post
    Code:
    For i As Integer = 0 To DGV.RowCount - 1
    
        Dim imgBytes() As Byte = TryCast(DGV.Rows(i).Cells(17).Value, Byte())
    
        If Not imgBytes Is Nothing Then
    
            Dim Ms As New MemoryStream(imgBytes)
    
            e.Graphics.DrawImage(Image.FromStream(Ms), 150, 20, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
        Else
    
            e.Graphics.DrawRectangle(Pens.Black, 150, 20, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
    
        End If
    Next
    in my DGV first fow and 3 row has image and if I select first row to 4th row then

    after debug code, all pages has 3rd row image.

    if I slect first and second rows only then both page has only first row image ( second row has no image)

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How Can I Print Images from DataGridView ?

    Quote Originally Posted by .paul. View Post
    Don't forget you have hardcoded x, y values there. Your images will all be drawn in the same place...
    You need to use a variable for the y position of your images...

    Code:
    Dim y as Integer = 20
    
    For i As Integer = 0 To DGV.RowCount - 1
    
        Dim imgBytes() As Byte = TryCast(DGV.Rows(i).Cells(17).Value, Byte())
    
        If Not imgBytes Is Nothing Then
    
            Dim Ms As New MemoryStream(imgBytes)
    
            e.Graphics.DrawImage(Image.FromStream(Ms), 150, y, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
        Else
    
            e.Graphics.DrawRectangle(Pens.Black, 150, y, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
    
        End If
    
        y += CInt(PictureBox1.Height / 3.78) + 12
    
    Next

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    Re: How Can I Print Images from DataGridView ?

    Quote Originally Posted by .paul. View Post
    You need to use a variable for the y position of your images...

    Code:
    Dim y as Integer = 20
    
    For i As Integer = 0 To DGV.RowCount - 1
    
        Dim imgBytes() As Byte = TryCast(DGV.Rows(i).Cells(17).Value, Byte())
    
        If Not imgBytes Is Nothing Then
    
            Dim Ms As New MemoryStream(imgBytes)
    
            e.Graphics.DrawImage(Image.FromStream(Ms), 150, y, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
        Else
    
            e.Graphics.DrawRectangle(Pens.Black, 150, y, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
    
        End If
    
        y += CInt(PictureBox1.Height / 3.78) + 12
    
    Next
    thanks for your quick reply but I think so I couldnt explain What I want.

    I try to Print Each record into difrent printdocument page.

    for example; first record (include image) to printdocument first page, Second record ( not include image and instade of image draw rectangle) to second pritdocument page,
    3rd record to 3rd Printdocument page...

    Thats why. I want to print all selected DGV rows into difrent pages.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How Can I Print Images from DataGridView ?

    Code:
    Dim i As Integer = 0
    
    Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
    
        For x As Integer = i To DGV.RowCount - 1
    
            Dim imgBytes() As Byte = TryCast(DGV.Rows(x).Cells(17).Value, Byte())
    
            If Not imgBytes Is Nothing Then
    
                Dim Ms As New MemoryStream(imgBytes)
    
                e.Graphics.DrawImage(Image.FromStream(Ms), 150, 20, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
    
            Else
    
                e.Graphics.DrawRectangle(Pens.Black, 150, 20, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
    
            End If
    
            i = x + 1
            Exit For
    
        Next
    
        If i < DGV.RowCount Then        
            e.HasMorePages = True
        Else
            i = 0
        End If
    
    End Sub

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    Re: How Can I Print Images from DataGridView ?

    Quote Originally Posted by .paul. View Post
    Code:
    Dim i As Integer = 0
    
    Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
    
        For x As Integer = i To DGV.RowCount - 1
    
            Dim imgBytes() As Byte = TryCast(DGV.Rows(x).Cells(17).Value, Byte())
    
            If Not imgBytes Is Nothing Then
    
                Dim Ms As New MemoryStream(imgBytes)
    
                e.Graphics.DrawImage(Image.FromStream(Ms), 150, 20, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
    
            Else
    
                e.Graphics.DrawRectangle(Pens.Black, 150, 20, CSng(PictureBox1.Width / 3.78), CSng(PictureBox1.Height / 3.78))
    
            End If
    
            i = x + 1
            Exit For
    
        Next
    
        If i < DGV.RowCount Then        
            e.HasMorePages = True
        Else
            i = 0
        End If
    
    End Sub
    I am sorry... even I select many pages then result is just only one empty page.

    dont know why.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How Can I Print Images from DataGridView ?

    The code I posted should work. All you’d need to change would be the Handles pd.PrintPage clause…

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How Can I Print Images from DataGridView ?

    The code I posted in #9… I edited it after posting. Check you’re using the latest code.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    Re: How Can I Print Images from DataGridView ?

    thank you so much it works now. god bless you.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How Can I Print Images from DataGridView ?

    Ok no problem. Sorry for not reading your OP right, but as you see there were several errors to fix there. Don't forget to mark your thread RESOLVED

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width