C# 分割 HTML 和 图片数据 - 使用 byte[] 读取文件
可以使用字符串的 IndexOf 方法来找到图片的起始位置,然后使用 Substring 方法截取出图片部分,最后将剩余的部分作为纯 html 处理。具体代码如下:
byte[] fileBytes = File.ReadAllBytes(filePath);
string html = Encoding.UTF8.GetString(fileBytes);
int imgStartIndex = html.IndexOf('<img');
if (imgStartIndex > 0)
{
string pureHtml = html.Substring(0, imgStartIndex);
// 处理纯 html 部分
string imgPart = html.Substring(imgStartIndex);
// 处理图片部分
}
else
{
// 没有图片,处理全部 html 部分
}
原文地址: https://www.cveoy.top/t/topic/l03f 著作权归作者所有。请勿转载和采集!