Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以html格式从项目文件夹加载图像以在webcontrol中显示_C#_Visual Studio 2012_Webbrowser Control_Windows Applications - Fatal编程技术网

C# 以html格式从项目文件夹加载图像以在webcontrol中显示

C# 以html格式从项目文件夹加载图像以在webcontrol中显示,c#,visual-studio-2012,webbrowser-control,windows-applications,C#,Visual Studio 2012,Webbrowser Control,Windows Applications,我有一本书的windows应用程序。我在webcontrol中显示章节文本,因为它是html格式的。在章节文本中有一些图像,我将它们添加到名为images的文件夹中。如何在html标记中加载指定的图像 chapterText += "<div align=center><img src=@'Images\\" + imageName + ".jpg' width='350' vspace='5'></div>"; chapterText+=”; 我尝试将这

我有一本书的windows应用程序。我在webcontrol中显示章节文本,因为它是html格式的。在章节文本中有一些图像,我将它们添加到名为images的文件夹中。如何在html标记中加载指定的图像

chapterText += "<div align=center><img src=@'Images\\" + imageName + ".jpg' width='350' vspace='5'></div>";
chapterText+=”;
我尝试将这些图像添加为资源,但无法将它们添加到上面的html代码中。
请给出建议。

您需要指定图像的完整路径。如果图像存储在本地,则允许您提供正常的windows路径:

在您的情况下,一个可能的解决方案是将
Images
目录放在应用程序文件夹中,并使用访问它,如下所示:

// 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);
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);
//获取图像所在的目录,例如:c:\temp\app\images\
var imagePath=Path.Combine(Application.StartupPath,@“Images”);
//创建映像路径,例如:c:\temp\app\images\one.jpg
var imageFile=Path.Combine(imagePath,String.Format(“{0}.jpg”,imageName));
//使用它
chapterText+=String.Format(“,imageFile);
您有两个选项可以使用可执行文件交付图像:

  • 创建一个为您处理依赖项的安装程序包(例如MSI安装程序包或ClickOnce)
  • 交付归档文件(包含EXE的ZIP文件和包含图像的图像文件夹)
  • ,当应用程序启动时,将它们存储在临时目录中并从那里检索,当应用程序退出时,从临时目录中删除图像
  • 将图像嵌入EXE,然后使用
最后一个选项的解决方案可能如下所示:

// 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);
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);
var image=@“d:\temp\image.jpg”;
//使用MemoryStream从嵌入式ressource加载图像
var imageAsBase64=Convert.ToBase64String(File.ReadAllBytes(image));
var webBrowser=新的webBrowser();
//将图像直接嵌入html代码,无需从文件系统加载
webBrowser.DocumentText=String.Format(“,imageAsBase64);

输出与上图相同。这样,您就不再需要images目录(或临时目录)。

非常感谢,它可以工作,但前提是我复制了bin\debug\images中的images文件夹。我现在关心的是,当我将项目作为一个可执行文件book.exe完成时,它是否也会起到同样的作用,答案是否有用。我已经延长了我的答复。