Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting image source in RDLC report dynamically

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?

like image 616
Mike L Avatar asked Mar 28 '11 05:03

Mike L


People also ask

How do I add a dynamic image in Rdlc?

Insert an image in that column, set its properties now. Image source should be EXTERNAL. Now again go to image properties and set "Use This Image" to [filepath] as it the name of the column in our dataset that is holding the path of the image.


2 Answers

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.

like image 55
Mike L Avatar answered Oct 15 '22 09:10

Mike L


I was having the same problem, however the accepted solution didn't quite work for me. Turns out that I needed to set EnableExternalImages to true in addition to providing the path in URI format and setting my Image.Value to =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);
like image 37
jackal Avatar answered Oct 15 '22 10:10

jackal