1002 Pass an Image to a Report - majorsilence/Reporting GitHub Wiki
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 in to the report into a base64 string. Then 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 on the report:
=Convert.FromBase64String(Parameters!image_parameter_name.Value)
C# source code:
using System.IO;
using fyiReporting.RDL;
using fyiReporting.RdlViewer;
using fyiReporting.Data;
Majorsilence.Reporting.RdlViewer.RdlViewer rdlView = new Majorsilence.Reporting.RdlViewer.RdlViewer();
rdlView.SourceFile = new Uri("\path\to\your\report.rdl");
// Setup the image to be passed into the report
Bitmap yourImage = (Bitmap)ResizeImage("\\the\Path\to\your\image.jpg", 75, 50);
string base64Image = Image2Base64(yourImage);
// This is where you pass your image into the report.
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 public string Image2Base64(Image value)
{
if (value == null)
{
return "";
}
//map the image as png format
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
value.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
//convert the image map to base 64 string
string base64 = Convert.ToBase64String(memoryStream.ToArray());
memoryStream.Close();
return base64;
}
// Help functions to get correct image size
public static Image ResizeImage(string file, int width, int height)
{
Image image = Image.FromFile(file);
return ResizeImage(image, width, height);
}
public static Image ResizeImage(Image imgToResize, int width, int height)
{
if (imgToResize == null)
{
return null;
}
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)width / (float)sourceWidth);
nPercentH = ((float)height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}