Black Friday is here! Get up to 30% off on Devart products!
ends in   {{days}}
Days
{{timeFormat.hours}}
:
{{timeFormat.minutes}}
:
{{timeFormat.seconds}}

Adding External Images to an SQL Data Report

There are two ways to store images in an SQL Server database:

  • directly in a database
  • store a link to an image file that is on the hard drive
Table with Images

When it comes to the report creation via SQL Report Builder, embedding images, stored in a database is not a big deal. That is why, in this article we will consider adding images, stored on a hard drive, to a report.

1. Open the Toolbox window by clicking Toolbox on the Data Report toolbar.

2. Select Picture Box and drop it onto the report's Detail Band.

Picture Box Embedding

3. Switch to the Scripts view.

4. On the top-left corner of the document, select Picture Box from the drop-down combo box.

5. On the top-right corner of the document, select Before Print from the drop-down combo box. The following script will be generated:

private void pictureBox1_BeforePrint(...

6. Insert the following string into the generated method template:

((XRPictureBox)sender).Image = Image.FromFile(GetCurrentColumnValue("Logo").ToString());

NOTE: "Logo" is the name of the column containing a file path. It should be listed within the Data Source window.

Script

NOTE: if a source table includes both ? records with and without image references, the string should include the following clauses:

if (GetCurrentColumnValue("Logo").ToString()!="")

else ((XRPictureBox)sender).Image = null;

Script, if there are missing pictures

5. Switch to the Preview mode to see the result.

Report with Images

Summary

External images are represented in an SQL database as URL references to image files stored on a hard drive. This way to store images has certain advantages, e.g. it enables to minimize the size of a database. However, if there is a necessity to create a report that should include external image files, they would still be just URLs in the report. With dbForge Studio for SQL Server, the problem can be easily solved by altering the report's script.