使用资源映像作为 RDLC 参数中的值 [英] Using a Resource Image as the value in RDLC Parameter

查看:34
本文介绍了使用资源映像作为 RDLC 参数中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像作为参数传递给 RDLC 报告中的图像.我尝试使用以下方法:

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;

但是当我运行它时,显示器总是显示红色的 X.我设法完成了这项工作,但图像的来源与 .exe 处于同一级别,而不是在其中.

我也尝试创建一个 BitmapImage,但 ReportParameter() 只接受字符串.

有没有办法让它起作用?或者我应该将它复制到 .exe 文件旁边?

注意事项:

  • 图片来源设置为External

  • default_product_img.png 位于 Resources 文件夹内,并具有 ResourceBuild Actionp>

  • 参数名称设置为Use this image:

解决方案

将图像作为位图保存到内存流中,然后将内存流转换为 base64 字符串.将此字符串传递给参数并将该参数用作图像.在 RDLC 中,将图像源设置为数据库,并确保 MIME 类型与您将位图保存到内存流的方式正确匹配.

string paramValue;using (var b = new Bitmap("文件路径或位图的新属性")) {使用 (var ms = new MemoryStream()) {b.save(ms, ImageFormat.Png);paramValue = ConvertToBase64String(ms.ToArray());}}

或者,如果您想将其保留为外部文件,请将图像源设置为 rdlc 中的外部文件,并将图像的路径传递为 file://c:\site\resources\default_product_img.png 它将需要是绝对路径,您可以使用 Server.MapPath 将 Web 相对路径转换为绝对本地路径,然后只需确保路径开头有 file://,以便报表引擎知道它是本地路径.

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:

解决方案

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.

这篇关于使用资源映像作为 RDLC 参数中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆