Skip to main content Skip to footer

Display Images Using DataSet Provider in Page Based Reports

The addition of Page Based Reports has not only increased the functionality offered by ActiveReports but has also provided the developers an option to chose from different ways of creating reports. Even though the base of the Page Based Reports is derived from Data Dynamics Reports tool, it is relatively new for the customers who are upgrading from previous versions of ActiveReports. Images play an important role when it comes to designing reports. In this blog article, I would explain how to display an image using the dataset provider. However, before we move ahead with the implementation, I would like to provide some information on how different sources work in conjunction with the Image control available for the Page Based Report. Image Values in Page Based Reports can be interpreted in one of the following three ways depending on the Source value used.

  • If the Source is set to Database, then the Image's Value expression should evaluate to a Byte Array containing the contents of the image. If you are creating the images through code, you can use System.Drawing.Image.Save to produce a suitable Byte Array. For this blog I am focusing on this approach.
  • If the Source is set to External, then the Image's Value expression should evaluate to a string which will be used to find the image on the file system, network, or over the HTTP protocol (it is routed through the ResourceLocator functionality, so you could also create your own URI prefix and retrieve images that way).
  • Finally, if the Source is set to Embedded, then the Image's Value expression should evaluate to a string containing the name of an embedded image in the report or master report. Embedded images can be modified through the data explorer window or through the Report Properties smart panel.

In this blog, we will consider a DataTable with single column that contains the image data and use it as the report data source. The report designer already has a datasource added at design time with its provider set to DataSet. On thing to note is that we are using the LocateDataSource event in this case to assign the datatable as the datasource for the report:


MemoryStream ms = new MemoryStream();  
dynamic img = Image.FromFile("..\\\..\\\Images\\\" + this.comboBox1.SelectedText);  
img.Save(ms, img.RawFormat);  
ms.Position = 0;  
int len = Convert.ToInt32(ms.Length);  
dynamic bytes = new byte[len];  
ms.Read(bytes, 0, len);  
dt = new DataTable();  
dt.Columns.Add(new DataColumn("img", typeof(byte[])));  
dynamic row = dt.NewRow();  
row[0] = bytes;  
dt.Rows.Add(row);  

string file_name = "..\\\..\\\Report1.rdlx";  
GrapeCity.ActiveReports.PageReport pageReport = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(file_name));  
GrapeCity.ActiveReports.Document.PageDocument pageDocument = new GrapeCity.ActiveReports.Document.PageDocument(pageReport);  
pageDocument.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(pageDocument_LocateDataSource);  
viewer1.LoadDocument(pageDocument);  

void pageDocument_LocateDataSource(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)  
{  
  args.Data = dt;  
}  

In addition to this, following properties for the Image control should be set on the designer: Image.Value="img" Image.Source=DataBase Image.MIMEType=img/jpg A sample application demonstrating this functionality can be downloaded from the following links. Once the application is run, select the image to be displayed from the combobox and click on the ShowReport button. LoadImage_C# LoadImage_VB.NET

David,Image Values in Page Based Reports can be interpreted in one of three ways depending on which Source value is used.If the Source is set to Database, then the Image's Value expression should evaluate to a byte array containing the contents of the image. If you are creating the images through code, then using System.Drawing.Image.Save will produce a suitable byte array. I have attached a small sample application which demonstrates the functionality using this approach. I have used the dataset provider to get the image. If the Source is set to External, then the Image's Value expression should evaluate to a string which will be used to find the image on the file system, network, or over the http protocol (it is routed through the ResourceLocator functionality, so you could also create your own URI prefix and retrieve images that way). Finally, if the Source is set to Embedded, then the Image's Value expression should evaluate to a string containing the name of an embedded image in the report or master report. Embedded images can be modified through the data explorer window or through the Report Properties smart panel.

MESCIUS inc.

comments powered by Disqus