In this article I will explain with an example, how to display Binary Image i.e. Image saved in SQL Server database in RDLC Report in ASP.Net Core (.Net Core 7) MVC.
Note: For beginners in RDLC Report in ASP.Net Core (.Net Core 7), please refer my article RDLC Reports in ASP.Net Core.
 
 

Database

I have made use of the following table Employees with the schema as follows.
Display Binary Image from database in RDLC Report in ASP.Net Core
 
I have already inserted few records in the table.
Display Binary Image from database in RDLC Report in ASP.Net Core
 
Note: You can download the database backup file by clicking the download link below.
           Download SQL file
 
 

Configuring RDLC Report

For more details on how to configure RDLC Report in ASP.Net Core, please refer my article RDLC Reports in ASP.Net Core.
Finally, the RDLC Report will look as shown below.
Display Binary Image from database in RDLC Report in ASP.Net Core
 
 

Configuring Typed DataSet for displaying Image

By default, all the columns are of String data type, but since the Image is stored in binary format for the Photo column we need to change the data type to BYTE Array by right clicking on the Photo column and click on Add, then Column.
Display Binary Image from database in RDLC Report in ASP.Net Core
 
Then inside the properties window, change the DataType to System.Bytes[].
Display Binary Image from database in RDLC Report in ASP.Net Core
 
 

Adding Image Field in the Report Designer

First, we need to delete the Photo field by right clicking the field and selecting Delete option from the Context menu as shown below.
Display Binary Image from database in RDLC Report in ASP.Net Core
 
Once the Photo field is deleted, the Image object needs to be dragged from the Toolbox and dropped into the empty Photo column as shown below.
Display Binary Image from database in RDLC Report in ASP.Net Core
 
As soon as the Image object is dropped, the Image Properties dialog window will open.
In the General Tab of the window, the Image Source property needs to be set to Database, the Field property should be set to Photo column and the MIME Type property should be set as image/jpeg as shown below.
Display Binary Image from database in RDLC Report in ASP.Net Core
 
Then in the Size Tab, the Display property needs to be set to Original size option as shown below.
Display Binary Image from database in RDLC Report in ASP.Net Core
 
Finally, the RDLC Report will look as shown below.
Display Binary Image from database in RDLC Report in ASP.Net Core
 
 

Model

The Model class consists of following properties.
public class Employee
{
    public int EmployeeId { getset; }
    public string FirstName { getset; }
    public string LastName { getset; }
    public byte[] Photo { getset; }
}
 
 

Database Context

Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
Note: For beginners in ASP.Net Core 3.1 and Entity Framework, please refer my article ASP.Net Core 3.1: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core 3.1.
 
using Microsoft.EntityFrameworkCore;
using RDLC_Report_Binary_Images_DB_Core_MVC.Models;
 
namespace RDLC_Report_Binary_Images_DB_Core_MVC
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<Employee> Employees { getset; }
    }
}
 
 

Namespaces

You need to import the following namespaces.
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Reporting.NETCore;
 
 

Controller

The Controller consists of following Action methods.

Action method for handling GET operation

Inside this Action method, simply the View is returned.
 

Action method for handling POST operation

Inside this Action method, first the path of the Report is read using IWebHostingEnvironment interface.
Note: For more details about IWebHostEnvironment interface, please refer Using IWebHostEnvironment in ASP.Net Core.
          For more details on how to access Static Files in .Net Core, please refer my article Static Files (Images, CSS and JS files) in ASP.Net Core.
 
Then, the Report file is read using FileStream class method and records are fetched from the Employees table using Entity Framework and stored as IEnumerable collection.
Next, an object of LocalReport class is created and Stream object is passed as parameter to the LoadReportDefinition method.
Now, the object of IEnumerable collection is added in the DataSources property to the LocalReport class using ReportDataSource object.
Finally, the generated report is converted to BYTE Array using Render method of LocalReport class and returned as File to the browser.
public class HomeController : Controller
{
    private DBCtx Context { get; }
    private IWebHostEnvironment Environment;
    public HomeController(DBCtx _context, IWebHostEnvironment _environment)
    {
        this.Context = _context;
        this.Environment = _environment;
    }
 
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public IActionResult ViewReport()
    {
        string reportFilePath = string.Concat(this.Environment.WebRootPath, "\\Reports\\Report.rdlc");
        using (Stream reportDefinition = new FileStream(reportFilePath, FileMode.Open, FileAccess.Read))
        {
            IEnumerable dataSource = (from employee in this.Context.Employees
                                      select employee);
            using (LocalReport report = new LocalReport())
            {
                report.LoadReportDefinition(reportDefinition);
                report.DataSources.Add(new ReportDataSource("Employees", dataSource));
                byte[] reportData report.Render("PDF");
                return File(reportData, "application/pdf");
            }
        }
    }
}
 
 

View

The View consists of an HTML Form with following ASP.Net Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is ViewReport.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Inside the Form there is a Submit button which when clicked the Form is submitted to the ViewReport Action method.
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        body  { font-family:Arial; font-size:10pt; }
    </style>
</head>
<body>
    <form method="post" asp-controller="Home" asp-action="ViewReport"> 
        <input type="submit" value="View Report" />
    </form>
</body>
</html>
 
 

Screenshot

Display Binary Image from database in RDLC Report in ASP.Net Core
 
 

Downloads