Show Image dynamically in RDLC report in Web API Core 3.1

Viewed 1017

i am stuck and Please help me with to bind the Image in dynamically in RDLC report Web API core3.1 application, i set image "external" image but when i bind image getting error "EnableExternalImages property has not been set for this report." please help me.report generating fine.

Dictionary<string, string> parameters = new Dictionary<string, string>();
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        Encoding.GetEncoding("windows-1252");
        //LocalReport report = new LocalReport("Report3.rdlc");
        LocalReport report = new LocalReport("Report3.rdlc");
        parameters.Add("Image", @"file:///E:\\DemoRDLC2\\DemoRDLC2\\DemoRDLC2/FileImage/Car.PNG");
        var result = report.Execute(GetRenderType("pdf"), 1, parameters);
        return result.MainStream;

enter image description here

2 Answers

i tested it and it's works, here is my code :

first of all ,convert your image to base64string

string paramValue = "";
using (var b = new Bitmap(@"YOUR IMAGE")){
     using (var ms = new MemoryStream()){
         b.Save(ms, ImageFormat.Bmp);
         paramValue = Convert.ToBase64String(ms.ToArray());
       }
    }

then add Base64String value to your parameters dictionary:

parameters.Add("Key1", "value1");
parameters.Add("Key2", "value2");
parameters.Add("image", paramValue);

RDLC image propertise: enter image description here

PDF file Result:

enter image description here

I just got it. I use Microsoft.ReportViewer.NetCore

The image has to be tif or eml. I have used tif.

Read this link: https://github.com/lkosson/reportviewercore#supported-rendering-formats

This is the code to print in a new page, from the sample in github:

        private IActionResult PrepareReport(string renderFormat, string extension, string mimeType)
    {
        string pathRdlc = $"{this._env.ContentRootPath}\\Reports\\Report.rdlc";
        string pathPhoto = new Uri($"{this._env.WebRootPath}\\Photos\\Photo1.tif").AbsoluteUri;

        using var report = new LocalReport();

        var parameters = new[] { new ReportParameter("pPhoto", pathPhoto) };

        byte[] fileBytes = System.IO.File.ReadAllBytes(pathRdlc);
        MemoryStream rs = new MemoryStream(fileBytes);

        report.EnableExternalImages = true;
        report.LoadReportDefinition(rs);
        report.SetParameters(parameters);

        var rpt = report.Render(renderFormat);
        return File(rpt, mimeType);
    }

To download file do this: return File(rpt, mimeType, FileName + extension);

I hope I've helped

Related