[英]Dynamically change an image in a Crystal Report at runtime
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. 我正在使用VisualStudio 2005附带的Crystal Reports。我想在运行时更改报表上显示的图像,理想情况是通过构建图像文件的路径,然后在报表上显示该图像。
Has anyone been able to accomplish this with this version of Crystal Reports? 有没有人能够使用此版本的Crystal Reports实现此目的?
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. 当然,此解决方案要求您通过DataSet将数据推送到报表中。 I've always felt this was a hack at best. 我一直认为这是一个充其量的黑客。 I really wish that image parameters were a possibility with CR. 我真的希望图像参数是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. 编辑:值得注意的是,如果要将水晶报表绑定到普通旧对象,则需要为报表公开byte []属性以将其视为图像。
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. [我已经找到了一个通过C#Object属性使用字节数组的解决方案 - 请参阅单独的答案。 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). 这是我所看到的建议(但我在C#-2005和C#-2008都试过并失败了)。
I had no luck with this approach. 我对这种方法没有运气。 The image appears when I first design the form (step 2). 第一次设计表格时会出现图像(步骤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. 似乎CR缓存某种不会消失的图像。 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). 我可以删除CR-Designer中的OLE对象链接,但如果我重新创建它,我总会得到一个与原始图像大小相同的黑盒子(即使我改变了image.bmp的大小)。
I finally reached a solution using the byte[]
tip posted here by Josh. 我终于使用Josh发布的byte[]
提示达成了解决方案。
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). 如果您使用普通的旧C#对象来填充Crystal Reports,则此解决方案适用(请参阅http://www.aspfree.com/c/a/C-Sharp/Crystal-Reports-for-Visual-Studio-2005-in -CSharp /有关此方法的信息)。
In your C# class, insert the following code: 在C#类中,插入以下代码:
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. 现在,使用“验证数据库”选项更新CR中的C#对象映射。 You should then see the Bitmap
property as a CR field. 然后,您应该将Bitmap
属性视为CR字段。 Just drag it onto the form. 只需将其拖到表单上即可。 It will be of type IBlobFieldObject. 它的类型为IBlobFieldObject。 When you run, you should see your image. 当你跑步时,你应该看到你的形象。
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 尝试结合使用包含图像路径的参数和本页教程: http : //www.idautomation.com/crystal/streaming_crystal.html
Then in step #8, use the parameter instead of a hard-coded path. 然后在步骤#8中,使用参数而不是硬编码路径。
You can also use a conditional formula to set an image's location. 您还可以使用条件公式来设置图像的位置。 See Crystal Reports: Dynamic Images . 请参阅Crystal Reports:动态图像 。
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. 选中Suppress框,然后单击公式按钮,显示为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. 在公式窗口中,我只写了COMPANY <> 1100,这意味着每次为1100以外的公司运行发票时,1100图形将被抑制。
Hopefully this makes life easier... 希望这会让生活更轻松......
The current version of Crystal Reports (for Visual Studio 2012+) that I use with Visual Studio 2015 supports this function. 我在Visual Studio 2015中使用的当前版本的Crystal Reports(适用于Visual Studio 2012+)支持此功能。 Follow the following steps: 请遵循以下步骤:
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 " if({@ isDonor} =“1”)然后“ http://www.ny.org/images/aaf/picture1.jpg ”否则“ http://www.ny.org/images/aaf/picture2.jpg “ And you're done! 而且你已经完成了!
Just like Josh said.. You will have to push the image with a dataset. 就像Josh说的那样..你必须用数据集来推动图像。 Or, put the image into a database table once and pull it in many times with a subreport. 或者,将图像放入数据库表一次,并使用子报表多次拉出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.