Using a Resource Image as the value in RDLC Parameter

独自空忆成欢 提交于 2021-02-09 11:42:35

问题


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:


回答1:


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());
    }
}

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.



来源:https://stackoverflow.com/questions/44694032/using-a-resource-image-as-the-value-in-rdlc-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!