0
votes

IM USING VB.NET - SQL AND CRYSTAL REPORTS

I'm trying to display an image into a crystal report's report but the image is displayed 20 % of the time, sometimes it works sometimes it doesn't.

Here is my Code: This is how I pass the parameters to the Crystal Report.

    Dim ds As New MyDS
    ds.Tables(0).Rows.Clear()

    All of the variables are strings and the Image ones are string Paths like this: C:\folder1\folder2\image.jpg   

    ds.Tables(0).Rows.Add(date, timeIn, timeOut, Load, noTrailer, noBox, seal, coment, nameDriver, company, plates, oficial, imagen1, imagen2, imagen3)

    Dim cr As New MyReporte

    CrystalReportViewer1.ReportSource = cr

    cr.SetDataSource(ds.Tables(0))

Here is how I add the image to the Crystal Report:

1.- Add an image to the report (Insert | Picture) as a place holder (I picekd a green circle).

2.- right click image

3.- select Format Graphic…

4.- select Picture tab

5.- click the conditional-formula button (looks like x+2)

6.- set the formula’s text to the name of the formula or parameter field that contains the image’s URL

7.- save the formula and click the OK button

8.- Save the report

Also, I added to the report the 'image' string to see if the path is correct.

1.- Here is the code of the image in crystal reports

2.- Here is how it looks in designer mode

3.- Here is the way it looks once I run the report for a specific row in the db

Notice how in the last image the links I'm sending to the report through the dataset are correct: "z:\folder1\folder2\folder3(name of this folder is the id of the row in the DB)\name of the image.jpg"

Also, the image displaying in the last image is the green image I used as place holder instead of the image in the path I sent.

1

1 Answers

0
votes

Heres how I fixed this:

I converted my image to byte array then created a byte array column on my dataset and finally dragged and dropped the byte array field into my crystal report.

Pros: - Dynamic images on Crystal Report

Cons: - Huge loss of Quality

Step by Step

  1. Use this function to convert images to a byte array:

    Public Shared Function GetBinary(ByVal image As Image, ByVal format As ImageFormat) As Byte()
    
    Using ms As New System.IO.MemoryStream
        If (format Is Nothing) Then
            format = image.RawFormat 
        End If
        image.Save(ms, format)
        Return ms.ToArray()
    End Using
    
    End Function here
    

In order to use this Function you have to include this 2:

Imports System.Drawing
Imports System.Drawing.Imaging

2.- I used this function like this:

imageVariable = GetBinary(pictureBox.Image, ImageFormat.Png)

This converts my image from the PictureBox to a Byte Array and stores it in a variable of type Byte()

You can also give this function a Path to an image and turn it to byte Array like this:

imageVariable = GetBinary(Image.FromFile("C:\PATHTOYOURIMAGE"), ImageFormat.Png)

Site that i used for reference: Click Me