-
Sep 18th, 2021, 06:04 PM
#1
Thread Starter
Lively Member
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
-
Sep 18th, 2021, 06:37 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 18th, 2021, 06:50 PM
#3
Thread Starter
Lively Member
Re: How Can I Print Images from DataGridView ?
Originally Posted by .paul.
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...
-
Sep 18th, 2021, 07:05 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 18th, 2021, 07:26 PM
#5
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 19th, 2021, 01:43 PM
#6
Thread Starter
Lively Member
Re: How Can I Print Images from DataGridView ?
Originally Posted by .paul.
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)
-
Sep 19th, 2021, 01:54 PM
#7
Re: How Can I Print Images from DataGridView ?
Originally Posted by .paul.
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 19th, 2021, 02:11 PM
#8
Thread Starter
Lively Member
Re: How Can I Print Images from DataGridView ?
Originally Posted by .paul.
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.
-
Sep 19th, 2021, 02:19 PM
#9
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 19th, 2021, 02:34 PM
#10
Thread Starter
Lively Member
Re: How Can I Print Images from DataGridView ?
Originally Posted by .paul.
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.
-
Sep 19th, 2021, 02:40 PM
#11
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 19th, 2021, 02:43 PM
#12
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 19th, 2021, 02:53 PM
#13
Thread Starter
Lively Member
Re: How Can I Print Images from DataGridView ?
thank you so much it works now. god bless you.
-
Sep 19th, 2021, 03:15 PM
#14
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|