4

I'm trying to pass an image as a parameter to an Image in a RDLC Report. I tried using the following:

string imgPath = new Uri("pack://application:,,,/Resources/default_product_img.png").AbsoluteUri;

string imgPath = new Uri(AppDomain.CurrentDomain.BaseDirectory + "pack://application:,,,/Resources/default_product_img.png").AbsoluteUri;

string imgPath = new Uri("/Resources/default_product_img.png").AbsoluteUri;

string imgPath = new Uri(AppDomain.CurrentDomain.BaseDirectory + "/Resources/default_product_img.png").AbsoluteUri;

string imgPath = new Uri("pack://application:,,,/Resources/default_product_img.png", UriKind.Absolute).AbsoluteUri;

string imgPath = new Uri(HttpContext.Current.Server.MapPath("~/Resources/default_product_img.png")).AbsoluteUri;

string imgPath = new Uri(HostingEnvironment.MapPath("~/Resources/default_product_img.png")).AbsoluteUri;

but the display always show the red X when I run it. I managed to make this work, but the source of the image is in the same level as the .exe and not inside it.

I also tried creating a BitmapImage, but ReportParameter() only accepts strings.

Is there a way for this to work? Or should I just copy it beside the .exe file?

Things to Note:

  • The image source is set as External

  • default_product_img.png is inside Resources folder and has a Build Action of Resource

  • The parameter name is set as the value in Use this image:

Carl Binalla
  • 5,393
  • 5
  • 27
  • 46

1 Answers1

7

Take the image as a bitmap and save it to a memory stream then convert the memory stream into a base64 string. Pass this string into the parameter and use that parameter as the image. In the RDLC set the image source to be database and make sure the mime type is a correct match for how you saved the bitmap to the memory stream.

string paramValue;
using (var b = new Bitmap("file path or new properties for bitmap")) {
    using (var ms = new MemoryStream()) {
        b.save(ms, ImageFormat.Png);
        paramValue = ConvertToBase64String(ms.ToArray());
    }
}

enter image description here

Or if you want to keep it as an external file set the image source to be external in the rdlc and pass the path to the image as file://c:\site\resources\default_product_img.png it will need to be an absolute path and you can use Server.MapPath to convert the web relative path to an absolute local path then just make sure you have file:// at the beginning of the path so the report engine knows it's a local path.

David
  • 3,653
  • 2
  • 24
  • 26
  • External is invalid when using an image that is embedded in the exe file? – Carl Binalla Jun 22 '17 at 15:34
  • 1
    I missed the part about it being an embeded resource. I suggest then to use the database option and read it into a bitmap object (following the first suggestion in my answer). – David Jun 22 '17 at 17:07
  • 1
    Embeded resources are already byte[], do only the b64 conversion part – Rafael Oct 07 '21 at 12:53