如何访问和输出C#to html代码中资源中的图像?
我想使用C#winforms生成并输出一个带有图像的html文件。我在Resources文件夹中有一些图像,但我无法将它们输出到html 有人能建议如何访问资源中的图像并将其输出到html文件吗 我的代码:如何访问和输出C#to html代码中资源中的图像?,c#,html,C#,Html,我想使用C#winforms生成并输出一个带有图像的html文件。我在Resources文件夹中有一些图像,但我无法将它们输出到html 有人能建议如何访问资源中的图像并将其输出到html文件吗 我的代码: using System; using System.Windows.Forms; using System.IO; namespace winformToHtml { public partial class Form1 : Form { public Fo
using System;
using System.Windows.Forms;
using System.IO;
namespace winformToHtml
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_report_Click(object sender, EventArgs e)
{
StreamWriter sWrite = new StreamWriter("C:\\report.html");
sWrite.WriteLine("<html>");
sWrite.WriteLine("<body>");
sWrite.WriteLine("<p> <img src= 'Resources/image.png' height='10%' width='5%' > </p>");
sWrite.WriteLine("<body>");
sWrite.WriteLine("<html>");
sWrite.Close();
}
}
}
使用系统;
使用System.Windows.Forms;
使用System.IO;
名称空间winformToHtml
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效btn\u报告\u单击(对象发送者,事件参数e)
{
StreamWriter sWrite=newstreamwriter(“C:\\report.html”);
sWrite.WriteLine(“”);
sWrite.WriteLine(“”);
sWrite.WriteLine(“”);
sWrite.WriteLine(“”);
sWrite.WriteLine(“”);
sWrite.Close();
}
}
}
您的问题缺少故事中的部分。我假设您在应用程序中嵌入了资源,并且您编写了一个html文件,该文件应该在应用程序外部工作。html文件引用嵌入在可执行文件中的图像
您需要将其复制到写入html文件的目录
如果它们没有嵌入,您可以使用File.Copy()
如果希望html文件是自包含的(即没有外部引用),还可以使用base64在html中编写图像,请参见@Haga的答案 您可以使用IMG base 64语法渲染图片:
public static String Base64Encoded(Image image)
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, ImageFormat.Jpeg);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
然后:
sWrite.WriteLine("<p><img src='data:image/jpeg;base64," + Base64Encoded(Resource.Image) + "' height='10%' width='5%' > </p>");
sWrite.WriteLine(“”);
我在image.Save(image,ImageFormat.Jpeg)行中遇到错误;。我认为应该是image.Save(m,ImageFormat.Jpeg);Base64Encoded(Resource.Image)是否为Base64Encoded(Resources.Image)?当我将图像导出为html时,底部图像的某些部分丢失。你能建议我如何解决这个问题吗?