How to pass an image to report from code?

Stimulsoft Reports.WEB discussion
Vicent
Posts: 5
Joined: Mon Jan 29, 2018 7:02 pm

How to pass an image to report from code?

Post by Vicent »

Hello, I'm trying to populate and render a report with an image coming from a dynamic source, in an ASP.NET Core controller, but I don't know how to pass the image to the report, neither as a parameter nor as variable.

I've tried this:

1) In the dictionary I create a new variable (image type) called "Logo"
2) In the .mrt report I insert an image an set its source to an "Expression":

Code: Select all

{Logo}
3) Then, in the controller:

Code: Select all

                var fi = env.ContentRootFileProvider.GetFileInfo($@"\Images\logo1.png");
                if (fi.Exists)
                {
                    FileStream stream = new FileStream(fi.PhysicalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true);
                    report.Dictionary.Variables["Logo"].ValueObject = Image.FromStream(stream);
                    return StiNetCoreReportResponse.ResponseAsPdf(report);
                }
                else return BadRequest();
But it doesn't work, it shows nothing... What's the correct way to pass an image to a report as an stream or something similar (image comes from a secured source, so it cannot come from an url or file path)

Thanks in advance
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: How to pass an image to report from code?

Post by Alex K. »

Hello,

We need some additional time to check the issue. We will let you know about the result.

At the current moment, you can use the following code instead using variable:

Code: Select all

var image = report.GetComponentByName("Image1") as StiImage;
image.Image = Image.FromStream(stream);
Thank you.
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: How to pass an image to report from code?

Post by Alex K. »

Hello,

At the current moment, the NET Core does not contain System.Drawing.Image.
Please try to use the following code for set Image type for variable:

Code: Select all

report.Dictionary.Variables["Logo"].Type = typeof(Stimulsoft.System.Drawing.Image);
report.Dictionary.Variables["Logo"].ValueObject = Stimulsoft.System.Drawing.Image.FromStream(stream);
Thank you.
Vicent
Posts: 5
Joined: Mon Jan 29, 2018 7:02 pm

Re: How to pass an image to report from code?

Post by Vicent »

Hello Alex,

I was already using the Stimulsoft.System.Drawing namespace ant it didn't work, but your trick to set the type of the variable previous to assign it has done the trick!

Now it works! Thank you
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: How to pass an image to report from code?

Post by Alex K. »

Hello

We are always glad to help you!
Please let us know if you need any additional help.

Thank you.
Vicent
Posts: 5
Joined: Mon Jan 29, 2018 7:02 pm

Re: How to pass an image to report from code?

Post by Vicent »

Hello Alex K.,

I've just updated to Stimulsoft Web 2018.2.3 and the Image type has dissapeared and throws a compilation error...

Stimulsoft.System.Drawing doesn't contain Image type

Have you moved the type to another namespace? I haven't seen this change on the changelogs.
Could you please point me in the right direction?

Thanks in advance
Lech Kulikowski
Posts: 6243
Joined: Tue Mar 20, 2018 5:34 am

Re: How to pass an image to report from code?

Post by Lech Kulikowski »

Hello,

In the last version, we use Microsoft official graphic assemblies, you can use System.Drawing.Image

Thank you.
Vicent
Posts: 5
Joined: Mon Jan 29, 2018 7:02 pm

Re: How to pass an image to report from code?

Post by Vicent »

Hello Lech, I supposed that, and indeed I tried it before writing here, but unfortenately it didn't worked, image wasn't loaded, and there was no error or exception thrown...
Lech Kulikowski
Posts: 6243
Joined: Tue Mar 20, 2018 5:34 am

Re: How to pass an image to report from code?

Post by Lech Kulikowski »

Hello,

We have found the issue and fix it. Problem with ImageConverter, the fix will be available in the next release build.
At the current version, as a workaround, please check sample:
https://drive.google.com/file/d/1tnkRkH ... sp=sharing

Thank you.
Vicent
Posts: 5
Joined: Mon Jan 29, 2018 7:02 pm

Re: How to pass an image to report from code?

Post by Vicent »

No problem, just reverted to 2018.2.2 and works ok...

I'll wait for the new release. Thanks!
Post Reply