Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I use VB.NET under visual studio 2012.
I have a form that contain a datagridview, a picture and a button print.
I created the cristal report viewer to print the content of datagridview and the picture, for that, I added a datatable in witch I added fields for the datagridview columns and a field for the Image ( the Image field type is bytes()).
The problem is that all datagridview fields are displayed but the image is not displayed.

What I have tried:

When I click on the button Print, the datagridview content is shown in the Cristal Report Viewer but the image is not shown ( I already drag the Image field in the datatable to the cristal report viewer as I did with the other fields ).
I tried another method, which is to create a pivcture in the Cristal Report View >> Right Clic >> Format Object >> Picture >> X2 >> double clic on the image field in the datatable . Also with this method I got error " a blob field can not be used in a formula".
This is the code of the button PRINT :
VB
Dim dt As New DataTable
   With dt
       .Columns.Add("code")
       .Columns.Add("productnom")
       .Columns.Add("category")
       .Columns.Add("supplier")
       .Columns.Add("qte")
       .Columns.Add("Image", GetType(Byte()))

   End With
   pathUrl = Application.StartupPath & "\logo.png"
   Dim img As Image = Image.FromFile(pathUrl)
   Dim dr As DataRow = dt.NewRow()
   dr("Image") = imageToByteArray(img) ' The function imageToByteArray is already created

   dt.Rows.Add(dr)

   For Each dgr As DataGridViewRow In Me.Datagridview1.Rows
       dt.Rows.Add(dgr.Cells(0).Value, dgr.Cells(1).Value, dgr.Cells(2).Value, dgr.Cells(3).Value, dgr.Cells(4).Value)
   Next

   Dim reportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument
   reportDocument = New CrystalReport1
   reportDocument.SetDataSource(dt)
   FormPrint.CrystalReportViewer1.ReportSource = reportDocument
   FormPrint.ShowDialog()
   FormPrint.Dispose()


And the code of the function imageToByteArray is :

VB
Public Function imageToByteArray(imageIn As System.Drawing.Image) As Byte()
        Dim ms As New MemoryStream()
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
        Return ms.ToArray()
        
End Function


Plz, if someone could help me . Thanks
Posted
Updated 21-Jul-17 3:39am

1 solution

I resolved the problem by changin the ImageFormat in the function
imageToByteArray ()
in PNG instead of GIF because cristal reports don't accept the GIF format.
the updated function is :
VB
Public Function imageToByteArray(imageIn As System.Drawing.Image) As Byte()
        Dim ms As New MemoryStream()
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.PNG)
        Return ms.ToArray()
        
End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900