c# - 使用资源图像作为 RDLC 参数中的值

标签 c# resources 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文件夹并有一个 Build ActionResource

  • 参数名称设置为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());
    }
}

enter image description here

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

关于c# - 使用资源图像作为 RDLC 参数中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44694032/

相关文章:

c# - Request.Url.Host 与 Request.Url.Authority

当应用程序用作库时,Java 访问资源文件时出现问题

c# - WinForms 报表查看器 : mouse wheel scrolls two pages instead of one

c# - 禁用折线图类别按字母顺序分组排序

C# UWP 如何在 xaml 中显示 string[]?

c# - ASP.NET Core 中 Startup.cs 中的 Kestrel 关闭函数

http - jhipster 错误 400 BAD REQUEST 并保存 $resource

c# - 从资源中声明一个 const 字符串

reporting-services - 如何为分组列创建总计行

c# - 为什么 DispatcherTimer 在某个随机时间后停止触发?