Accessing pictures with crystal report from file system

degraft

Member
Joined
Feb 17, 2009
Messages
7
Programming Experience
1-3
Hello All,

With all my projects so far, I have been storing my pictures in the MSSQL Server making them easily accessible from crystal reports by just dropping a the image field from my stored procedure onto the new report.

Now because of the slow image retrieval performance of my applications, I wish to let crystal report pick them from a folder on the server or elsewhere.

I wished to be helped on how to go by this method using VB.NET 2005 AND MSSQL SERVER 2005.

Thanks so much.

Regards,
Degraft
 
Here's how I did it:
Add a dataset to your project, I called it "ds_data" (ds_data.xsd). Add a Datatable (Image), add 2 columns (Name,Photo).

Change Datatype of Name column to System.String and change the datatype of the Photo column to System.Byte()

In your report, right clikc Database Fields and choose Database Expert. Browse to
Project Data - ADO.NET Datasets - <applicationname>.ds_data - Image. Now add this to
selected tables.

On your left you'll see under "Database Fields" that the dataset is there with the 2 columns. Now drag the picture field on your report and give it a
nice name. :D

Now that you've got everything prepped, let's move on to the code. :)

VB.NET:
Dim dsImgRpt As New ds_data
Dim m_imagereport As New rapport
Dim dr As ds_data.ImageRow = dsImgRpt.Image.NewImageRow

Private Function GetImageData(ByVal fileName As String) As Byte()

        Dim fs As System.IO.FileStream = _
                    New System.IO.FileStream(fileName, _
                    System.IO.FileMode.Open, System.IO.FileAccess.Read)
            Dim br As System.IO.BinaryReader = New System.IO.BinaryReader(fs)
            Return (br.ReadBytes(Convert.ToInt32(br.BaseStream.Length)))

End Function

'Filling report with data
...
dsImgRpt.Image.Rows.Clear()
rpt1.DataDefinition.FormulaFields.Item("ubALIsectnr").Text = "'" & txtPOnr.Text & "'"
...
dr.Name = strPath & "\" & strItem & ".jpg" ' (full file path)
...
If subs.exist(strPad & "\" & strItem & ".jpg") Then
   dr.Photo = GetImageData(strPath & "\" & strItem & ".jpg")
Else
   dr.Photo = GetImageData(strPath & "\" & "nopic.jpg")
End If

dsImgRpt.Image.Rows.Add(dr)
rpt1.SetDataSource(dsImgRpt)
Hth

#grmbl#
 
Last edited:
Back
Top