13
votes

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?

8

8 Answers

8
votes

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.

2
votes

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.

2
votes

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).

1
votes

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

1
votes

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.

1
votes

Another option that I've found useful is inserting the pictures you would like to use. Position the graphic accordingly, then right-click the graphic and go to Format Graphic > Common. Check the Suppress box, then click the formula button, shown as x-2. Once in the formula window, simply add the code for determining whether the graphic should be suppressed or not.

In my case, I was building one invoice template for multiple entities. In the formula window, I simply wrote COMPANY <> 1100 which meant that every time the invoice was run for a company other than 1100, the 1100 graphic would be suppressed.

Hopefully this makes life easier...

1
votes

The current version of Crystal Reports (for Visual Studio 2012+) that I use with Visual Studio 2015 supports this function. Follow the following steps:

  1. Insert a picture into your report. This will serve as your placeholder.'
  2. Right click your picture and choose Format Object Right Click Image
  3. Select the Picture tab and the press the formula button Picture Tab
  4. A formula window will open. Enter a formula that will find your pictures as links.

    if({@isDonor}="1") then "http://www.ny.org/images/aaf/picture1.jpg" else "http://www.ny.org/images/aaf/picture2.jpg" Formula Editor And you're done!

0
votes

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.