13

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?

Phillip Wells
  • 7,402
  • 9
  • 43
  • 41
Keith
  • 922
  • 2
  • 10
  • 12

8 Answers8

8

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
  • 2,708
  • 4
  • 24
  • 25
  • 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 Feb 23 '09 at 21:49
  • 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 Feb 24 '09 at 03:32
  • Eureka! I had tried a "Bitmap" property, but hadn't run across the byte[] CR trick in the reading I have done. – e-holder Feb 24 '09 at 15:58
  • @JoshBush I'm printing the report from command line, and the picture is not showing up, I think i'm missing something. – Eli Ekstein Sep 18 '14 at 16:26
2

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

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
e-holder
  • 1,504
  • 4
  • 20
  • 33
  • not sure what the "link" option does. I always thought the insert would embed the picture into the report. – dotjoe Feb 21 '09 at 03:51
  • 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 Feb 23 '09 at 21:14
  • The same fails in VB.NET VS 2010, @e-holder -- The original image always stays. – Keith Jul 11 '14 at 13:54
  • This is working. Simply replace the image in the path you've selected (manually or via code). Thanks! – smukamuka Aug 25 '15 at 09:03
  • Not sure what has changed in the slight variations of CR versions, but now the image works, but can't be updated via a passed parameter. It ignores the parameter and prompts the user for the location. Argggg – Keith May 27 '16 at 20:32
2

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
  • 1,504
  • 4
  • 20
  • 33
  • Been a long time since I've used Crystal or VB. Don't know if I can help. What part of this are you needing help translating to VB? The byte array? The streams/readers? The property? The object mapping in CR? – e-holder Jul 15 '14 at 14:36
  • Almost the entire thing, but it's okay, I ended up just using Microsoft Reports and dynamic images worked perfectly. 11.5 Crystal Reports seems like a piece of crap when it comes to the Graphic Location option. It completely ignores whatever you put in there. All of these solutions I've found online for CR, were for older versions. Thank you very much for responding to an old post though. – Keith Jul 15 '14 at 18:11
1

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.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Mikey
  • 2,837
  • 2
  • 18
  • 17
  • 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 Sep 24 '08 at 17:47
  • Unlike the other @Keith -- I am using CR 11.5 and that route still doesn't work. The image never changes from the original. – Keith Jul 11 '14 at 13:56
1

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

craig
  • 25,664
  • 27
  • 119
  • 205
1

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

Adam
  • 45
  • 4
1

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!

Glenn Gordon
  • 1,266
  • 1
  • 14
  • 24
  • Using this process and simply setting the formula to a parameter allows for setting the full URL from code, allowing us the most flexibility and least .rpt tweaking. – brichins Aug 06 '19 at 17:32
0

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
  • 26,242
  • 5
  • 63
  • 77