Dynamically change an image in a Crystal Report at runtime

后端 未结 8 1240
天命终不由人
天命终不由人 2021-02-04 08:08

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 ima

相关标签:
8条回答
  • 2021-02-04 08:53

    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.

    0 讨论(0)
  • 2021-02-04 08:53

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

    0 讨论(0)
  • 2021-02-04 08:56

    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.

    0 讨论(0)
  • 2021-02-04 08:58

    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.

    0 讨论(0)
  • 2021-02-04 09:02

    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.

    0 讨论(0)
  • 2021-02-04 09:03

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

    0 讨论(0)
提交回复
热议问题