Friday, April 10, 2009

To change the image dynamically in crystal reports

If you are using Crystal Reports XI or later you can do this programatically using the 'Graphic Location' conditional formula.

To do this, perform the following steps:

1. Open the report in the Crystal Reports Designer.

2. Create a formula called "Location" that contains a temporary path string to an image file. For example, "C:\Images\Image1.jpg".

3. Right-click the image on the report and click 'Format Graphic'. The 'Format Editor' dialog box appears.

4. In the 'Picture' tab, click the 'Graphic Location' conditional formula button. The Format Formula Editor appears.

5. Call the "Location" formula from this conditional formula.

6. Save changes to the report.

7. In your application, you can now change the location of the image file at run time, by modifying the text in the "Location" formula programmatically using the following code:

myReport.DataDefinition.FormulaFields.Item("Location").Text = "C:\Images\Image2.jpg"

You can now view the image successfully when previewing your report in the application.

12 comments:

Unknown said...

Does this work with VB.net 2005 and CR IX R2.

VS is complaining about my formula. It says:

"Error in formula "PartImage".
'C:\PartImages\8050.tif'
The remaining text does not appear to be part of the formula."

I setup crystal just like you have, but the only thing I changed is in my app

"Dim strNewImagePath As String
strNewImagePath = My.Settings.ImagePath & "\" & strPN.Substring(0, 4) & ".tif"
cryRpt.DataDefinition.FormulaFields.Item("PartImage").Text = strNewImagePath"


The path to the new image pulls in correctly, but the app errors out at this line of code when I print

" cryRpt.PrintToPrinter(1, False, 1, 1)"

Thanks

vijay2040 said...

Thankyou very much for this post it's really works 4 me. Thanks a lot...

Unknown said...

HI.I'm Nehru.I got the same error above said..But my application is same in c# windows application..Please reply this...

Oriion said...

you have to include the path into 'full path' , it should work :) . marconysac :)

Oriion said...

you have to include the path into 'fulpath'

Rick said...

myReport.DataDefinition.FormulaFields.Item("Location").Text = "C:\Images\Image2.jpg"

I had to wrap this Path string with "'" to send it, like this...

"'" + "C:\Images\Image2.jpg"+ "'"

Anonymous said...

Excelent post.

Unknown said...

Thanks Srinivas! This helped me a ton! :)

>>Martin said...

Thank you so much, I was using an event to update the OLE object which was not the best solution !

3d programmer said...

Thanks a lot .

atiburrahman09 said...

reportDocument.Load(reportPath + "Report_Sales_Invoice.rpt");
dt = db.ExecuteDataTable("REPORT_GET_SALES_INVOICE_BY_ID", true);
reportDocument.SetDataSource(dt);
reportDocument.DataDefinition.FormulaFields.Item("Location").Text = (imagePath + "noimage.png");

this is my code. But the error i am getting is "Item can not be resolved". It's from "FormulaFields.Item".

Can you tell me why i am getting this?

atiburrahman09 said...

i searched in the net and found the solution. It has changed in CR 13. It's now like this:

reportDocument.DataDefinition.FormulaFields["Location"].Text = (imagePath + "noimage.png");

But the think is there is no error but the image does not change. The image i used earlier remains the same.