使用资源图像作为 RDLC 参数中的值
Using a Resource Image as the value in RDLC Parameter
我正在尝试将图像作为参数传递给 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:// 以便报表引擎知道它是本地路径.
我正在尝试将图像作为参数传递给 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:// 以便报表引擎知道它是本地路径.