Page 1 of 2

How to pass an image to report from code?

Posted: Mon Jan 29, 2018 7:24 pm
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

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

Posted: Thu Feb 01, 2018 10:11 pm
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.

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

Posted: Fri Feb 02, 2018 7:42 am
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.

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

Posted: Fri Feb 02, 2018 10:58 am
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

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

Posted: Fri Feb 02, 2018 11:44 am
by Alex K.
Hello

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

Thank you.

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

Posted: Fri Aug 10, 2018 12:03 pm
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

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

Posted: Mon Aug 13, 2018 1:03 pm
by Lech Kulikowski
Hello,

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

Thank you.

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

Posted: Mon Aug 13, 2018 4:12 pm
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...

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

Posted: Tue Aug 14, 2018 12:10 pm
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.

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

Posted: Tue Aug 14, 2018 6:03 pm
by Vicent
No problem, just reverted to 2018.2.2 and works ok...

I'll wait for the new release. Thanks!