我正在尝试将图像作为参数传递给 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
文件夹并有一个Build Action
的Resource
参数名称设置为
Use this image:
中的值
最佳答案
将图像作为位图保存到内存流中,然后将内存流转换为base64字符串。将此字符串传递到参数中并将该参数用作图像。在 RDLC 中,将图像源设置为数据库,并确保 mime 类型与您将位图保存到内存流的方式正确匹配。
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());
}
}
或者,如果您想将其保留为外部文件,请在 rdlc 中将图像源设置为外部,并将图像路径作为 file://c:\site\resources\default_product_img.png 传递,这将需要是绝对路径,您可以使用 Server.MapPath 将 Web 相对路径转换为绝对本地路径,然后只需确保路径开头有 file://,以便报表引擎知道它是本地路径。
关于c# - 使用资源图像作为 RDLC 参数中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44694032/