Load Web Images in C1Report

C1Report is a powerful reporting tool, which provides a large scope of customizations that we can do on the ReportDefinition, create using this control. By ReportDefinition, I don't mean only the report data sources, or the text values in fields, but what we are going talk about in this blog is adding completely new fields to an existing ReportDefinition. What we will add in these fields would be images captured from a web Link! This approach is quite beneficial to developers who are using creating reporting solutions, for web based applications. Incase, one already has a set of Images that are being used, as consistent styles over various pages, and it is required to show similar formatting in a report being generated, off one of these pages in the application, I would say, why store the same images, needed in Report Design, why not use them off the web pages instead? But then that is just me, there can be other uses for this technique as well. How do we go about achieving this is a simple two part process,

Step 1:

This requires us to read the web link in an http webrequest object, render the web link without visually opening it anywhere, in a webresponse object, and further convert the response into a webstream. Once we have the webstream it is quite simple to convert it to an image object. We can use the following code to implement the above,

C1WebReport1.Report.Load(<path of ReportDefinition file>, <Report Name>);  
 string \_URL = "http://judturner.com/new\_work\_gal/bio-cycle\_2-2008.jpg";  

 // Create the requests.  
 System.Net.HttpWebRequest \_HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(\_URL);  

 _HttpWebRequest.AllowWriteStreamBuffering = true;  

 System.Net.WebResponse \_WebResponse = \_HttpWebRequest.GetResponse();  
 System.IO.Stream \_WebStream = \_WebResponse.GetResponseStream();  
 System.Drawing.Image _tmpImage = null;  

 //Save Image in image object  
 \_tmpImage = System.Drawing.Image.FromStream(\_WebStream);  
 _tmpImage.Save(@"c:\\test\\test.png");//This line of code is not required it is just to test of image is saved correctly or not  

Step 2

Render the image object as a field in existing report

This is again just another approach to show how we can use the image in a C1Report object, for the convenience of use in this sample, I have loaded an existing report and added the image object got above to the footer section in the report, but on the other hand we can use this approach in a report that we are creating from scratch, completely through code. Adding the a field though, uses the same code, as below, not withstanding, how we had created the initial report,

//Add Image field to the report  
 Field Image1;  
 Section s1 = C1WebReport1.Report.Sections[SectionTypeEnum.PageFooter];  
 Image1 = s1.Fields.Add("Image1", "", 50, 50, 4440, 1480);  
 Image1.Width = 4440;  
 Image1.Height = 1480;  
 //Image1.BackColor = System.Drawing.Color.Blue;  
 Image1.PictureScale = PictureScaleEnum.Scale;  
 Image1.Picture = _tmpImage;  

The above code can be placed any where depending on the use. In the attached sample below, I have used it in the Page_Load event, so as soon as the web site is run , this report is render with an image in footer, taken from a web link. Download Sample


GrapeCity Developer Tools
comments powered by Disqus