您需要指定圖像的完整路徑。如果圖像存儲在本地,你被允許提供一個正常的Windows路徑:
在你的情況下,一個可能的解決辦法是把Images
目錄在你的應用程序文件夾,並使用Application.StartupPath property訪問,是這樣的:
// fetch directory where images reside, e.g.: c:\temp\app\images\
var imagePath = Path.Combine(Application.StartupPath, @"Images");
// create image path, e.g.: c:\temp\app\images\one.jpg
var imageFile = Path.Combine(imagePath, String.Format("{0}.jpg", imageName));
// use it
chapterText += String.Format("<div align=center><img src='{0}' width='350' vspace='5'></div>", imageFile);
你有幾個選項與可執行提供圖像:
- 創建一個安裝程序包,處理您的依賴(例如MSI安裝程序包或ClickOnce的)
- 遞送歸檔(含有ZIP的EXE並且圖像與圖像文件夾)
- embed the images into the EXE,在應用程序啓動時,將它們存儲在一個臨時目錄,並從那裏檢索它們,當退出應用程序,刪除臨時目錄中的圖片
- 嵌入圖像轉換爲EXE文件,然後使用他們直接使用的最後一個選項可能看起來是這樣的URI scheme
一個解決方案其嵌入到HTML:
var image = @"d:\temp\image.jpg";
// use MemoryStream to load the image from the embedded ressource
var imageAsBase64 = Convert.ToBase64String(File.ReadAllBytes(image));
var webBrowser = new WebBrowser();
// embed the image directly into the html code, no need to load it from the file system
webBrowser.DocumentText = String.Format("<html><body><img src='data:image/jpg;base64,{0}' /></body></html>", imageAsBase64);
輸出與上圖相同。這樣你就不再需要圖像目錄(或臨時圖像)。
非常感謝感謝,它的工作原理,但只有當我複製bin \ debug \ Images中的Images文件夾。我現在關心的是,當我將該項目定爲可執行文件book.exe – 2014-09-21 17:52:15
時,它的工作原理是一樣的,答案很有幫助。我已經延長了我的答案。 – pasty 2014-09-21 18:16:55