Setting image source in RDLC report dynamically

Mike L picture Mike L · Mar 28, 2011 · Viewed 43.5k times · Source

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.

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.

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?

Answer

Mike L picture Mike L · Apr 19, 2011

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

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