在 RDLC 报告中动态设置图像源 [英] Setting image source in RDLC report dynamically

查看:28
本文介绍了在 RDLC 报告中动态设置图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用与 Visual Studio 2010 捆绑在一起的客户端报告功能.我定义了一个 RDLC 文件,目前在报告顶部嵌入了用于品牌推广的图像.该图像是用户公司的徽标.它与报告数据没有任何关系......它只是一个标题.

I'm using the client-side reporting capabilities that are bundled in with Visual Studio 2010. I've got an RDLC file defined, currently with embedded images for branding purposes at the top of the report. The image is the logo for the user's company. It has nothing whatsoever to do with the report data... it's just a title.

我希望能够打破对嵌入图像的依赖,因为我开始不得不扩展应用程序.相反,我希望能够动态设置图像.不幸的是,似乎没有参数类型支持这一点.

I'd like to be able to break the dependency on embedding the images, as I'm beginning to have to scale the app. Instead, I'd like to be able to dynamically set the image. Unfortunately there is no parameter type that seems to support this.

我已经考虑将源从嵌入切换到外部,并可能在程序启动时发出徽标的图像文件(徽标作为资源嵌入到单独的程序集中),然后将其称为通用命名的源文件.我不确定我有多喜欢这个选项,因为它似乎是一个黑客.在测试显式设置路径图像时,我也遇到错误,实际上是说对象未设置为实例.例如,我什至尝试将其设置为 D: est.jpg,但在设计时却遇到了该错误……所以我更不愿意尝试这个选项.

I've looked at switching the source from embedded to external, and perhaps emitting an image file of the logo at program launch (the logo's are embedded as resources in a separate assembly), then referring to it as a generically-named file for the source. I'm not sure how much I like this option, as it seems a hack. I also get an error when testing explicitly set path images, effectively saying the object is not set to an instance. For example, I've even tried to set it to D: est.jpg, and gotten that error at design time... so I'm more reluctant to try this option.

我还查看了从 RDLC 文件中调用引用程序集中的类,但我似乎无法让它工作.看起来我可以引用一个程序集,然后通过一个名为 Code 的特殊对象进行调用.因为我的类是静态的,应该是Code.className.method,但是好像不行.

I've also looked at calling a class in a referenced assembly from within the RDLC file, but I can't seem to get that to work. It looks like I can reference an assembly, then call via a special object called Code. Because my class is static, it should be Code.className.method, but that doesn't seem to work.

我也考虑过将标题分解为子报表,但我仍然认为我没有解决我的依赖问题.它仍然需要相同数量的维护.

I've also considered breaking the title into a subreport, but I still don't think I've solved my dependency problem. It would still require the same amount of maintenance.

我应该提到我使用对象作为我的数据源.我应该选择什么?我是否遗漏了一些明显的东西?

I should mention that I'm using objects as my datasource. What option should I go with? Am I missing something obvious?

推荐答案

由于对此事没有替代(或任何!)意见,我已经更进一步并提出了一个可行的解决方案.

As there are no alternate (or any!) opinions on the matter, I've moved further along and have come up with a working solution.

我选择创建徽标的按需文件,将其存储在临时位置.如果文件不存在,我会即时创建它.如果确实存在,我只是引用确实存在的图像.

I'm opting to create an on-demand file of the logo, storing it in a temp location. If the file doesn't exist, I'm creating it on the fly. If it does exist, I'm just referencing the image that does exist.

在 RDLC 报告中,我创建了一个名为 Path 的 Text 类型参数.接下来,在图像的属性中,我将徽标图像从嵌入更改为外部并将使用此图像"设置为参数:[@Path].

In the RDLC report, I've created a parameter called Path of type Text. Next, in the properties for the Image, I've changed the logo image from embedded to external and set "Use this image" to be the parameter: [@Path].

然后,在代码中,我将文件路径作为 Path 参数传入.但是我之前出错的地方是路径必须是一个 URL,我一直试图传递磁盘上的位置.因此,该部分应如下所示:

Then, in the code I'm passing in the file path as the Path parameter. But where I had previously gone wrong is that the path has to be a URL and I had been attempting to pass the location on disk. So, that portion should look like this:

        ReportParameter paramLogo = new ReportParameter();
        paramLogo.Name = "Path";
        paramLogo.Values.Add(@"file:///C:UsersMikeAppDataLocalTempLogo.png");
        reportViewer.LocalReport.SetParameters(paramLogo);

我会说 MSDN 文档可能会好一点.值得称赞的是,有许多关于如何在更高层次上完成某事的详细文件.这篇文章有所帮助.它清楚地表明我需要一个指向路径的 URL,但直接在库中检查该属性会更容易.但是,找到较低级别的文档更难,而且成效也更低.这是报告图像对象的文章.设置感兴趣的属性的机会不多.

I will say that the MSDN documentation could be a little better. To their credit, there are many detailed documents about how to accomplish something at a higher level. This article helped. It clearly says that I needed a URL to the path, but it'd have been easier to examine that property directly in the library. However, finding the lower level documentation was harder and less fruitful. Here is the article for the Reporting Image object. There isn't much opportunity to set properties of interest.

这篇关于在 RDLC 报告中动态设置图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆