logo
small logo
  • Products
  • Buy
  • Support
  • Articles
  • Customer panel Support
    • en
    • ru
    • pt
    • es
    • de
    • pl
    • JP
    • ZH
  • Home
  • /
  • Articles
  • /
  • How to set a picture in a report from the user application code
  • Report generators family FastReport brief review

    March 23, 2020

    Without reporting it is impossible to carry on business in any sphere of life. Bureaucracy

    read more
  • How to use Online Designer in ASP .Net Core

    January 17, 2018

    One of the novelties by FastReport .Net 2018 was the adaptation of OnlineDesigner to the

    read more
  • How to use WebReport with ASP .Net Web Core application

    January 17, 2018

    Recently FastReport introduced a new library under the .Net - FastReport Core platform. This is

    read more
  • How to connect to SQLCe

    November 11, 2019

    Microsoft SQL Server Compact Edition is a simple local relational database that doesn't require installation,

    read more
  • How to disable printing reports and other items from the menu when viewing the report FastReport.Net

    February 29, 2020

    The main report viewing tool FastReport.Net is Viewer. This viewer has a rich toolkit for

    read more

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

January 8, 2020

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.

about product download buy
avatar
Dmitriy Fedyashov
Head of QA
.NET FastReport

Add comment
logo
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 901 N Pitt Str #325 Alexandria VA 22314
  • Buy
  • Download
  • Documentation
  • Testimonials
  • How to uninstall
  • Ticket system
  • FAQ
  • Tutorial Video
  • Forum
  • Articles
  • Our News
  • Press about us
  • Resellers
  • Our team
  • Contact us

© 1998-2021 by Fast Reports Inc.

  • Privacy Policy