none
ReportViewer programmatically set Image RRS feed

  • Question

  • I have a report with an image. I want to programmatically set the image.
    The users will choose which image they want in the report, so I have to programmatically set it.

    But how? Is there a way to do that?
    Get a life. Go party!
    Monday, October 26, 2009 6:29 PM

Answers

  • The easiest way to do this is to have a hidden parameter on the report that is used to set the source of an external image.

    - Set the Image ReportItem's Source property to "External"
    - Set up the images somewhere, on disk or on a webserver, depending on the situation
    - Add a hidden parameter to the report, called "imageFile" for example
    - Set the image's Value to use this parameter, something like  ="C:\Images\" + Parameters!imageFile.Value.
    - Before user views the report, set this parameter to the image they chose


            private void Form1_Load(object sender, EventArgs e)
            {
                ReportParameter param = new ReportParameter();
                param.Name = "imageFile";
                param.Values.Add(ImageMyUserChose);
                reportViewer1.LocalReport.SetParameters(new ReportParameter[] { param });
                this.reportViewer1.RefreshReport();
            }

    The example I chose is for the WinForms viewer, the WebForms viewer would still be the same idea, except you'd likely have the images stored on a server somewhere.



    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by JobaDiniz Tuesday, October 27, 2009 11:17 AM
    Monday, October 26, 2009 11:16 PM
  • HI JobaDiniz,
     
    Use the same code to the at the begining of the path add this string  "file:\\\"

    Ex: "file:\\\" & "C:\Document and Settings\MyPictures\winter.jpg". [OR] "file:\\\" & ReportParameter!param.value

    This will work!
     
    - Raja sekhar K

    Hope this answer will help you
    Tuesday, October 27, 2009 9:13 AM
  • It works, but before everything, you must add this line:

    this.reportViewer1.LocalReport.EnableExternalImages = true;

    Thanks.

    Get a life. Go party!
    • Marked as answer by JobaDiniz Tuesday, October 27, 2009 11:17 AM
    Tuesday, October 27, 2009 11:17 AM

All replies

  • The easiest way to do this is to have a hidden parameter on the report that is used to set the source of an external image.

    - Set the Image ReportItem's Source property to "External"
    - Set up the images somewhere, on disk or on a webserver, depending on the situation
    - Add a hidden parameter to the report, called "imageFile" for example
    - Set the image's Value to use this parameter, something like  ="C:\Images\" + Parameters!imageFile.Value.
    - Before user views the report, set this parameter to the image they chose


            private void Form1_Load(object sender, EventArgs e)
            {
                ReportParameter param = new ReportParameter();
                param.Name = "imageFile";
                param.Values.Add(ImageMyUserChose);
                reportViewer1.LocalReport.SetParameters(new ReportParameter[] { param });
                this.reportViewer1.RefreshReport();
            }

    The example I chose is for the WinForms viewer, the WebForms viewer would still be the same idea, except you'd likely have the images stored on a server somewhere.



    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by JobaDiniz Tuesday, October 27, 2009 11:17 AM
    Monday, October 26, 2009 11:16 PM
  • Good call, but it doesn't work. I'm using WinForms, by the way.

    There's no error, however nothing happens.
    Get a life. Go party!
    Tuesday, October 27, 2009 1:59 AM
  • HI JobaDiniz,
     
    Use the same code to the at the begining of the path add this string  "file:\\\"

    Ex: "file:\\\" & "C:\Document and Settings\MyPictures\winter.jpg". [OR] "file:\\\" & ReportParameter!param.value

    This will work!
     
    - Raja sekhar K

    Hope this answer will help you
    Tuesday, October 27, 2009 9:13 AM
  • It works, but before everything, you must add this line:

    this.reportViewer1.LocalReport.EnableExternalImages = true;

    Thanks.

    Get a life. Go party!
    • Marked as answer by JobaDiniz Tuesday, October 27, 2009 11:17 AM
    Tuesday, October 27, 2009 11:17 AM
  • I have a image in the application directory- which I will update periodically.
    I want to load this image at runtime in my report.

    I am a beginner in C#. different posts confuse me.

    If possible within time your time limitations please post a consolidated procedure.

    Thank you.
    Thursday, December 3, 2009 5:05 AM
  • - Set the Image ReportItem's Source property to "External"
    - Set up the images somewhere, on disk or on a webserver, depending on the situation
    - Add a parameter to the report, called "imageFile" for example
    - Set the image's Value to use this parameter:  ="file:\\\" & Parameters!imageFile.Value.
    - Add this code on Load of the form that hold the reportViewer:

    private
     void
     Form1_Load(object
     sender, EventArgs e)
    {
    this .reportViewer1.LocalReport.EnableExternalImages = true ;
    ReportParameter param = new ReportParameter();
    param.Name = "imageFile" ;
    param.Values.Add(path_of_image);
    reportViewer1.LocalReport.SetParameters(new ReportParameter[] { param });
    this .reportViewer1.RefreshReport();
    }


    Get a life. Go party!
    • Proposed as answer by RoyMicro Thursday, December 3, 2009 11:03 AM
    Thursday, December 3, 2009 10:12 AM
  • Thanks a lot.
    It is working now. You realized an end to my last two days struggle.


    Thanks a lot
    Roy Thomas
    Thursday, December 3, 2009 11:02 AM
  • I must be missing something here.. if i drag/drop an Image onto my canvas, and wish to programmatically load a different image with your parameter example; how do I assign that specific Image.Value the param value?

    Example: I have an Image, it's name is imgDealerLogo, set to External, empty string.
    I use your code snippet to load a new image via the "imageFile" param... how then do I assign my imgDealerLogo.Value the new param value?

    Thanks much
    Thursday, January 21, 2010 7:39 PM
  • I didn't get it... You do realize that we are talking about ReportViewer, dont you?

    Add a parameter in the reportviewer.
    drag/drop an image onto reportviewer, doesnt matter which name you will name it, you can let the default name (the important name is the parameter's name). Set this image to external and set the Property called Value with the string that is in the code snippet.



    Get a life. Go party!
    Friday, January 22, 2010 1:14 AM
  • The easiest way to do this is to have a hidden parameter on the report that is used to set the source of an external image.

    - Set the Image ReportItem's Source property to "External"
    - Set up the images somewhere, on disk or on a webserver, depending on the situation
    - Add a hidden parameter to the report, called "imageFile" for example
    - Set the image's Value to use this parameter, something like ="C:\Images\" + Parameters!imageFile.Value.
    - Before user views the report, set this parameter to the image they chose


        private void Form1_Load(object sender, EventArgs e)    {      ReportParameter param = new ReportParameter();      param.Name = "imageFile";      param.Values.Add(ImageMyUserChose);      reportViewer1.LocalReport.SetParameters(new ReportParameter[] { param });      this.reportViewer1.RefreshReport();    }

    The example I chose is for the WinForms viewer, the WebForms viewer would still be the same idea, except you'd likely have the images stored on a server somewhere.



    This posting is provided "AS IS" with no warranties, and confers no rights.

    I'll take a try right now, I have the similar question.
    Sunday, January 16, 2011 3:18 AM
  • This sounds great and simple enough... but i am using C# Windows Forms... unfortunately the "Image" control isn't available... do you think it's possible to use this using the "Picture Box" control?

    Anthony Griggs

    Wednesday, November 28, 2012 7:43 PM
  • Thank you very much for this answer!

    Joao Paulo de Lima Braga

    Monday, June 2, 2014 12:50 PM