ActiveReports lets you modify images at run time. How you do it depends on the type of report you are using, so I’ll break it down by report type.

Page and RDL Reports

In Page reports, to modify images at run time we set the Source property of the Image control to External, and set the path to the image file in the Value property.

You can even use an expression in the Value property to modify the image at run time.

For example, in the following expression, when the value of the “ID” field is an odd number, ActiveReports displays “image\1.jpg” (located in the “image” folder of the project) and when it is an even number, it displays “image\2.jpg.”

=IIF(Fields!ID.Value mod 2 = 1, “image\1.jpg”, “image\2.jpg”)

Use an expression like this to display an image that isn’t in the project.

=IIF(Fields!ID.Value mod 2 = 1, “c:\data\1.jpg”, “c:\data\2.jpg”)

To learn more about the Image control, see the Image topic in our User Guide.

Section Reports

In Section reports, you can load an image into a Picture control at run time using System.Drawing.Image methods.

Using the example code and script below, you can load an image from a file and let ActiveReports display it in a Picture control placed in the Detail section.

‘Visual Basic

Private Sub Detail_Format(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Detail.Format
Picture1.Image = System.Drawing.Image.FromFile(“C:\a.JPG”)
End Sub
//C#

private void Detail_Format(object sender, System.EventArgs eArgs)
{
picture1.Image = System.Drawing.Image.FromFile(@”C:\\a.JPG”);
}
Script

private void detail_Format(object sender, EventArgs e)
{
((GrapeCity.ActiveReports.SectionReportModel.Picture)
rpt.Sections["detail"].Controls["picture1"]).Image =
System.Drawing.Image.FromFile(@”C://a.jpg”);
}

Caution:

Microsoft Support reports that the file is intentionally locked when you load an image file using the System.Drawing.Image.FromFile method at run time. The workaround is to use System.IO.FileStream. Check out the article below for specifics.

Image file is locked when you set the PictureBox Image property to a file

This article shows you how to load an image using the System.IO.FileStream class. However, if you use this in ActiveReports events (for example, Detail_Format), use a BitMap to encapsulate the image you loaded into the FileStream. Here is how.

  1. Load an image from the file path into a FileStream.
  2. Load the image from the FileStream and encapsulate it using a Bitmap.
  3. Set the Bitmap into the Image property of a Picture control.
‘ Visual Basic

Private Sub Detail_Format(ByVal sender As System.Object, ByVal e As _
System.EventArgs)
‘ Load an image file into a FileStream.
Dim fs As New System.IO.FileStream(“Specify the path to the image”, _
System.IO.FileMode.Open, System.IO.FileAccess.Read)
‘ Encapsulate the FileStream image in a Bitmap.
Dim bm As New Bitmap(System.Drawing.Image.FromStream(fs))
‘ Set the Bitmap to a Picture control on the PageHeader.
Me.Picture1.Image = bm
‘ Close the file.
fs.Close()
‘ CAUTION:
‘ An error occurs when you run bm.Dispose.
‘ To work around the image lock, do not run the Dispose method.
‘ bm.Dispose
End Sub
// C#

private void Detail_Format(object sender, System.EventArgs eArgs)
{
// Load an image file into a FileStream.
System.IO.FileStream fs = new System.IO.FileStream(“Specify the path to
the image”, System.IO.FileMode.Open, System.IO.FileAccess.Read);
// Encapsulate the FileStream image in a Bitmap.
Bitmap bm = new Bitmap(System.Drawing.Image.FromStream(fs));
// Set the Bitmap to a Picture control on the PageHeader.
this.Picture1.Image = bm;
// Close the file.
fs.Close();
// CAUTION:
// An error occurs when you run bm.Dispose.
// To work around the image lock, do not run the Dispose method.
//bm.Dispose();
}

With the following example code, you can set the loaded image into a Picture control in the PageHeader section.

‘ Visual Basic

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
‘ Load an image file into a FileStream.
Dim fs As New System.IO.FileStream(System.Windows.Forms.Application.StartupPath + _
“\\SampleImage.JPG”, System.IO.FileMode.Open, System.IO.FileAccess.Read)
‘ Load the FileStream image and set it to a Picture control in the PageHeader.
Dim rpt As New ActiveReport1
CType(rpt.Sections(“PageHeader”).Controls(“Picture1″), _
GrapeCity.ActiveReports.SectionReportModel.Picture).Image = _
System.Drawing.Image.FromStream(fs)
rpt.Run()
Viewer1.Document = rpt.Document
‘ Close the FileStream.
fs.Close()
End Sub
// C#

private void Form1_Load(object sender, EventArgs e)
{
// Load an image file into a FileStream.
System.IO.FileStream fs = new System.IO.FileStream(System.Windows.Forms.Application.StartupPath
+ “\\SampleImage.JPG”, System.IO.FileMode.Open, System.IO.FileAccess.Read);

// Load the image from the FileStream and set it into a Picture control in the
// PageHeader.
ActiveReport1 rpt = new ActiveReport1();
((GrapeCity.ActiveReports.SectionReportModel.Picture)rpt.Sections["PageHeader"].Controls["Picture1"]).Image
= System.Drawing.Image.FromStream(fs);
rpt.Run();
viewer1.Document = rpt.Document;

// Close the FileStream.
fs.Close();
}

When you show a report in the WebViewer, it causes a GDI+ exception and you can’t close the FileStream. So when you use the WebViewer, encapsulate the image FileStream object in a BitMap, and set it in the Image property.

‘ Visual Basic

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
‘ Load an image file into a FileStream.
Dim fs = New System.IO.FileStream(Server.MapPath(“”) ” _
\\SampleImage.jpg”, IO.FileMode.Open, ;System.IO.FileAccess.Read)
‘ Load the FileStream image and encapsulate it in a Bitmap.
Dim bmp As New Bitmap(System.Drawing.Image.FromStream(fs))
‘ Set the Bitmap to a Picture control in the PageHeader.
Dim rpt As New SectionReport1
CType(rpt.Sections(“PageHeader1″).Controls(“Picture1″), Picture).Image = bmp
‘ Run the report and show it in the WebViewer.
rpt.Run(False)
WebViewer1.Report = rpt
‘ Close the FileStream.
fs.Close()
End Sub
// C#

Protected void Page_Load(object sender, EventArgs e)
{
// Load an image file into a FileStream.
System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath(“”) _
+ “\\SmapleImage.jpg”, _
System.IO.FileMode.Open, System.IO.FileAccess.Read);
// Load the FileStream image and encapsulate it in a Bitmap.
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(System.Drawing.Image.FromStream(fs));
// Set the Bitmap to a Picture control in the PageHeader.
SectionReport1 rpt = new SectionReport1();
((GrapeCity.ActiveReports.SectionReportModel.Picture)rpt.Sections["pageHeader"]. _
Controls["picture1"]).Image = bmp;
// Run the report and show it in the WebViewer.
rpt.Run();
WebViewer1.Report = rpt;
// Close the FileStream.
fs.Close();
}

Tags