2017-06-22 27 views
2

我试图将图像作为参数传递给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:

回答

1

将图像作为位图并将其保存到内存流,然后将内存流转换为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外部外部文件和路径传递到图像文件:// C:\网站\ resources \ default_product_img.png它将需要是一个绝对路径,您可以使用Server.MapPath将Web相对路径转换为绝对本地路径,然后确保在路径开头处具有file://,以便报告引擎知道这是一条本地路径。

+0

使用内嵌在exe文件中的图像时外部无效? – Swellar

+1

我错过了关于它是嵌入式资源的部分。我建议然后使用数据库选项并将其读入位图对象(在我的答案中的第一个建议之后)。 – David