How to set a picture in a report from the user application code

2020-01-08

Quite often there is a need to set various images in the report depending on any conditions or input parameters. This problem was encountered by the user of the List & Label report generator:

Is there a way to programmatically insert an inline image into a List & Label document from .net code?

However, the user did not find a solution:

Unfortunately, it is not possible to insert an image from .NET code. This is only possible when working with a report in the List & Label designer.

Therefore, I want to show how this can be implemented in the FastReport.Net report generator.

So, when creating a report, we work directly with all of its objects - create them, add them to the report page, set properties. Consider a simple example of creating a report from the code of a user application with a picture inside.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Create instance of class Report
 Report report = new Report();
 //Add report page
 ReportPage page = new ReportPage();
 report.Pages.Add(page);
 page.CreateUniqueName();
 //App data band
 DataBand data = new DataBand();
 //Add data band to page
 page.Bands.Add(data);
 data.CreateUniqueName();
 data.Height = Units.Centimeters * 1; //Set band height
 //Create picture object
 PictureObject pic = new PictureObject();
 pic.Bounds = new RectangleF(0, 0, Units.Centimeters * 5, Units.Centimeters * 5); //Set object bounds
 pic.Image = new Bitmap("../../App_Data/snow_flake.ico"); //Set picture
 pic.Parent = data; //Set picture parent object
 pic.CreateUniqueName();
 report.Prepare(); //Build report
 report.Show(); //Show report

 This is a very simple example of a report with just one ‘data’ band. Since this report is entirely created in the program code, there is no problem to create an object with a picture and put it into the report. Creating a report from the code allows us to change it as much as we want, depending on the logic of the program.

Consider another case. Let's say you already have a report template created in a designer. You want to change the picture in the report depending on the logic of the program. In this case, the report template should already have a Picture object, and you'll just replace the picture itself from the user application code. Here's what the code will look like in the program:         

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 //Create report object
 Report report = new Report();
 //Load report template into the report obj
 report.Load("../../App_Data/Picture.frx");
 //Get picture object from the report template
 PictureObject pic = report.FindObject("Picture1") as PictureObject;
 //Set object bounds
 pic.Bounds = new RectangleF(0, 0, Units.Centimeters * 5, Units.Centimeters * 5);
 //Set the image
 pic.Image = new Bitmap("../../App_Data/snow_flake.ico");
 //Build report
 report.Prepare();
 //Show report
 report.Show();

 Here, we find an object with a picture in the template of the report and change its properties as desired.

And finally, the third version of the Picture object is from the built-in report script. The report script allows you to change the pattern and data in the report as you like. You can pre-add a Picture object to the template, or you can add it directly to the report script. Truly, limitless flexibility. There is no need to use a custom application to manage the contents of the report. This is a big plus for me, because there is no need to edit the application code.
The example for setting an image in the report script is extremely simple:

1
Picture1.Image = new Bitmap("C:/Users/Dimon/source/repos/PictureSetting/PictureSetting/App_Data/snow_flake.ico");

 You only need to decide on an event in which you want to change a picture, for example, you can use the BeforePrint event for a Picture object.

November 26, 2024

Installing FastReport on .NET 8.0 and Creating a Simple Report

The purpose of this article is to explain step by step how to install FastReport on .NET 8.0 and how to create a simple report. Taking the reporting process from the beginning, it will show how to connect, design and view reports.
November 20, 2024

Localization and Language Switching in FastReport VCL

FastReport VCL supports 40 languages for interface localization and allows you to change the language on the fly through menus or code, without recompilation.
November 01, 2024

New Features of the FastReport VCL Editor

We are considering new features of the report editor: extension lines, highlighting of intersecting objects, updated report and data trees.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.