2017-06-22 26 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