views:

11253

answers:

7

I'm using the Crystal Reports included with VisualStudio 2005. I would like to change the image that is displayed on the report at runtime ideally by building a path to the image file and then have that image displayed on the report.

Has anyone been able to accomplish this with this version of Crystal Reports?

+1  A: 

Try using a combination of using a parameter containing the path of the image and the tutorial on this page: http://www.idautomation.com/crystal/streaming_crystal.html

Then in step #8, use the parameter instead of a hard-coded path.

Mikey
Thanks but this solution is for a newer version of Crystal than we are using. We don't have the Graphic Location button in the image control properties.
Keith
yep, this would only work in CR 11 and up.
dotjoe
A: 

I also had this question (and voted yours up)!

[I have since found a solution using a byte array via a C# Object property - see separate Answer. Leaving this answer here for reference...]

Here's what I have seen suggested (but I tried and failed in both C#-2005 and C#-2008).

  1. Choose a directory and place a BMP there (e.g., "C:\Temp\image.bmp").
  2. From the CR-Designer a) Right-click->Insert->OLE Object... b) Select "Create from File" c) Check the "Link" checkbox d) Browse and pick the bmp defined in step 1 e) Click OK f) Place the image on the form.
  3. Overwrite/update the image at runtime in your C# code. In theory, since you inserted a Link to an image file, it will be updated when the form is refreshed.

I had no luck with this approach. The image appears when I first design the form (step 2). But at runtime, the image does not update for me. From this point forward, things get really odd. It seems that CR caches some sort of image that just won't go away. I can delete the OLE object link in CR-Designer, but if I recreate it, I always get a black box the same size as the original image (even if I change the size of image.bmp).

e-holder
not sure what the "link" option does. I always thought the insert would embed the picture into the report.
dotjoe
According to the "What's This" help for that checkbox, it is a choice to either embed the image, or link to it. I gather it is analogous to a windows "shortcut" to a file (or a filesystem link in unix). If it isn't embedding the image, it sure seems it should work.
e-holder
+3  A: 

At work we do this by pushing the image(s) into the report as fields of a datatable. It's not pretty, but it gets the job done. Of course, this solution requires that you push data into the reports via a DataSet. I've always felt this was a hack at best. I really wish that image parameters were a possibility with CR.

Edit: It's worth noting, if you are binding your crystal report to plain old objects you want to expose a byte[] property for the report to treat that as an image.

Josh Bush
I'm using a C# class per www.aspfree.com/c/a/C-Sharp/Crystal-Reports-for-Visual-Studio-2005-in-CSharp/. I've got a complex, custom drawn report with hundreds of fields. I don't see a good path from here to there. I looked at: codeguru.com/csharp/.net/net_general/toolsand3rdparty/article.php/c13253.
e-holder
If you are binding a Crystal Report to plain old objects, just expose a byte[] property. CR will see that as an image.
Josh Bush
Eureka! I had tried a "Bitmap" property, but hadn't run across the byte[] CR trick in the reading I have done.
e-holder
A: 

Just like Josh said.. You will have to push the image with a dataset. Or, put the image into a database table once and pull it in many times with a subreport.

dotjoe
A: 

I finally reached a solution using the byte[] tip posted here by Josh.

This solution applies if you are using a plain old C# Object to populate your Crystal Reports (see http://www.aspfree.com/c/a/C-Sharp/Crystal-Reports-for-Visual-Studio-2005-in-CSharp/ for info on this approach).

In your C# class, insert the following code:

private static byte[] m_Bitmap = null;

public byte[] Bitmap
{
   get
   {
      FileStream fs = new FileStream(bitmapPath, FileMode.Open);
      BinaryReader br = new BinaryReader(fs);
      int length = (int)br.BaseStream.Length;
      m_Bitmap = new byte[length];
      m_Bitmap = br.ReadBytes(length);
      br.Close();
      fs.Close();
      return m_Bitmap;
   }
}

Now, update your C# Object Mapping in CR using the "Verify Database" option. You should then see the Bitmap property as a CR field. Just drag it onto the form. It will be of type IBlobFieldObject. When you run, you should see your image.

e-holder
A: 

please am using VB6 and crystal report9 I need to change the image path in the runtime mode can any one help thanks

Haron
A: 

You can also use a conditional formula to set an image's location. See Crystal Reports: Dynamic Images.

Craig