.net 在 RDLC 报告中动态设置图像源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5454972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:24:47  来源:igfitidea点击:

Setting image source in RDLC report dynamically

.netimagereporting-servicesreporting

提问by Mike L

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.

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

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.

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

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:\test.jpg, and gotten that error at design time... so I'm more reluctant to try this option.

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

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.

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

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?

我应该提到我使用对象作为我的数据源。我应该选择什么?我错过了一些明显的东西吗?

回答by Mike L

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.

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

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

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

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:

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

        ReportParameter paramLogo = new ReportParameter();
        paramLogo.Name = "Path";
        paramLogo.Values.Add(@"file:///C:\Users\Mike\AppData\Local\Temp\Logo.png");
        reportViewer.LocalReport.SetParameters(paramLogo);

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 articlehelped. 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 articlefor the Reporting Image object. There isn't much opportunity to set properties of interest.

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

回答by Hymanal

I was having the same problem, however the accepted solution didn't quite work for me. Turns out that I needed to set EnableExternalImagesto true in addition to providing the path in URI format and setting my Image.Value to =Parameters!ReportLogo.Value.

我遇到了同样的问题,但是接受的解决方案对我来说并不完全有效。事实证明,我需要一套EnableExternalImages在除了真正提供的URI格式的路径和我Image.Value设置=Parameters!ReportLogo.Value

report.EnableExternalImages = true;
ReportParameter[]  parameters = new ReportParameter[3];
...
Uri pathAsUri =  new Uri(_info.LogoPath);
parameters[2] = new ReportParameter("ReportLogo", pathAsUri.AbsoluteUri);
report.SetParameters(parameters);