Skip to content

Pass an Image to a Report

Peter Gill edited this page Jun 7, 2026 · 9 revisions
dotnet add package Majorsilence.Reporting.RdlViewer

In this example an image is passed to a report to be displayed. You must convert any image you want to pass into the report into a base64 string. Once passed to the report as a string parameter your report will convert it back into an image.

In the report you need an image. Once the image is on the report right click it and select properties. In the Image tab in the source groupbox make sure the Database radio button is selected and the combobox is image/jpeg. In the function line below make sure you have =Convert.FromBase64String(Parameters!image_parameter_name.Value)

Image field expression on the report:

=Convert.FromBase64String(Parameters!image_parameter_name.Value)

Version 5

using Majorsilence.Reporting.Rdl;
using Majorsilence.Reporting.RdlViewer;

// One time per app instance
RdlEngineConfig.RdlEngineConfigInit();

var rdlView = new Majorsilence.Reporting.RdlViewer.RdlViewer();
await rdlView.SetSourceFile(new Uri(@"\path\to\your\report.rdl"));

// Setup the image to be passed into the report
string base64Image = Image2Base64(yourImage);

// Pass the image as a report parameter
rdlView.Parameters += string.Format("&image_parameter_name={0}", base64Image);

var rpt = await rdlView.Report();
await rpt.DataSets["DataSetNameInYourReport"].SetData(YourDataTable);
await rdlView.Rebuild();

static string Image2Base64(System.Drawing.Image value)
{
    if (value == null)
        return string.Empty;

    using var memoryStream = new System.IO.MemoryStream();
    value.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
    return Convert.ToBase64String(memoryStream.ToArray());
}

Version 4 (Legacy — .NET Framework / net48)

using System.IO;
using fyiReporting.RDL;
using fyiReporting.RdlViewer;
using fyiReporting.Data;

RdlViewer rdlView = new RdlViewer();
rdlView.SourceFile = new Uri(@"\path\to\your\report.rdl");

// Setup the image to be passed into the report
System.Drawing.Bitmap yourImage = (System.Drawing.Bitmap)ResizeImage(@"\the\Path\to\your\image.jpg", 75, 50);
string base64Image = Image2Base64(yourImage);

// Pass the image as a report parameter
rdlView.Parameters += string.Format("&image_parameter_name={0}", base64Image);

// This table needs to match the one you are setting
rdlView.Report.DataSets["DataSetNameInYourReport"].SetData(YourDataTable);

rdlView.Rebuild();


static string Image2Base64(System.Drawing.Image value)
{
    if (value == null)
        return "";

    using var memoryStream = new System.IO.MemoryStream();
    value.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
    return Convert.ToBase64String(memoryStream.ToArray());
}

static System.Drawing.Image ResizeImage(string file, int width, int height)
{
    System.Drawing.Image image = System.Drawing.Image.FromFile(file);
    return ResizeImage(image, width, height);
}

static System.Drawing.Image ResizeImage(System.Drawing.Image imgToResize, int width, int height)
{
    if (imgToResize == null)
        return null;

    int sourceWidth = imgToResize.Width;
    int sourceHeight = imgToResize.Height;

    float nPercentW = ((float)width / (float)sourceWidth);
    float nPercentH = ((float)height / (float)sourceHeight);
    float nPercent = nPercentH < nPercentW ? nPercentH : nPercentW;

    int destWidth = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);

    System.Drawing.Bitmap b = new System.Drawing.Bitmap(destWidth, destHeight);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b);
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
    g.Dispose();

    return b;
}

Clone this wiki locally