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.

12. August 2024

Erstellen und Installieren des Postgres-Plugins in FastReport .NET

In diesem Artikel wird beschrieben, wie Sie mit dem FastReport .NET-Plugin für den Berichtsdesigner aus Visual Studio über den NuGet-Server eine Verbindung zur Datenbank herstellen.
8. August 2024

Installation von FastReport .NET und ihren Komponenten unter Windows

Schritt für Schritt Anleitung zur Online- und manuellen Installation über den Registrierungscode FastReport .NET und seine Komponenten in Windows.
26. Juli 2024

Aktualisierung von HTMLObject als Plugin für FastReport .NET

Ausführliche Anleitung zur Verwendung des neuen HTMLObject-Plugins, das die Aufteilung des HTML-Doms in FastReport-Berichtsobjekte verwendet.
Fast Reports
  • 800-985-8986 (Englisch, die USA)
  • +4930568373928 (Deutsch)
  • +55 19 98147-8148 (Portugiesisch)
  • info@fast-report.com
  • 901 N Pitt Str #325 Alexandria VA 22314

© 1998-2024 Fast Reports Inc.