Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display image in crystal report from path given in database table.My code runs well but nothing is displayed in report.Below is my code.Please help me to find out the problem.
VB
Dim Con As OdbcConnection = New OdbcConnection
        Dim sql As String = "Select * from Image"
        Con.ConnectionString = connstring
        Dim da As OdbcDataAdapter = New OdbcDataAdapter(sql, Con)
        da.Fill(ds, "Image")
        ds.Tables("Image").Columns.Add("image_stream", Type.GetType("System.Byte[]"))


        For index As Integer = 0 To ds.Tables("Image").Rows.Count - 1
            If ds.Tables("Image").Rows(index)("image_path").ToString <> "" Then
                Dim s As String = ds.Tables("Image").Rows(index)("image_path").ToString
                If File.Exists(s) Then
                    LoadImage(ds.Tables("Image").Rows(index), "image_stream", s)

                End If
            End If
        Next index

        crDoc = New ImageReport
        crDoc.SetDataSource(ds.Tables("Image"))
        rptDoc.ReportSource = crDoc



Private Sub LoadImage(ByVal objDataRow As DataRow, ByVal strImageField As String, ByVal FilePath As String)
        Try
            Dim fs As System.IO.FileStream = New System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
            Dim Image() As Byte = New Byte(fs.Length) {}
            fs.Read(Image, 0, CType(fs.Length, Integer))
            fs.Close()
            objDataRow(strImageField) = Image
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub
Posted

1 solution

fill image in cell of data table
and make sure image is successfully read and inserted in to cell

C#
For index As Integer = 0 To ds.Tables("Image").Rows.Count - 1
            If ds.Tables("Image").Rows(index)("image_path").ToString <> "" Then
                Dim s As String = ds.Tables("Image").Rows(index)("image_path").ToString
                If File.Exists(s) Then

Drawing.Bitmap img = new Drawing.Bitmap(ds.Tables("Image").Rows(index)("image_path").ToString); //Replace string with your OpenFileDialog path.

MemoryStream ms = new MemoryStream();
     Image returnImage = Image.FromStream(ms);
System.Drawing.Image imageIn = new System.Drawing.Image();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);

                  ds.Tables("Image").Rows(index)("image_stream") =  ms.ToArray(); 

                End If
            End If
        Next index

Happy Coding!
:)
 
Share this answer
 
v3
Comments
Rachna0309 8-Jan-13 2:47am    
In crystal report,which field should I use to display image?
Aarti Meswania 8-Jan-13 2:53am    
use datatype varbinary(Max) or image in database
eg. your query should be
select *,convert(image,null) as image_stream from tabelname

so it will get an empty image column in crystal report
and you will read pass value using newly prepared datatable runtime
Rachna0309 8-Jan-13 3:01am    
Access 2003 does not have datatype varbinary or image.
Aarti Meswania 8-Jan-13 3:04am    
may be you will have ole datatype there
I prefer you to use xml for this report it will not make such problem
Rachna0309 8-Jan-13 2:50am    
Type of value has a mismatch with column typeCouldn't store <system.drawing.bitmap> in image_stream Column. Expected type is Byte[].

I get this error at line:
ds.Tables("Image").Rows(index)("image_stream") = img;

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