|
首先找到內(nèi)容里面第一個(gè)<img標(biāo)簽的位置,然后找到從這個(gè)起的第一個(gè)>的位置,得到第一張圖片的完整標(biāo)簽。
然后通過(guò)分隔空格得到圖片的各個(gè)屬性和屬性值,提取src的值就是圖片的地址
代碼如下:
復(fù)制代碼 代碼如下:
/// <summary>
/// 獲取文中圖片地址
/// </summary>
/// <param name="content">內(nèi)容</param>
/// <returns>地址字符串</returns>
public static string getImageUrl(string content)
{
int mouse = 0;
int cat = 0;
string imageLabel = "";
string imgSrc = "";
string[] Attributes;
do //得到第一張圖片的連接作為主要圖片
{
cat = content.IndexOf("<IMG", mouse);
mouse = content.IndexOf('>', cat);
imageLabel = content.Substring(cat, mouse - cat); //圖像標(biāo)簽
Attributes = imageLabel.Split(' '); //將圖片屬性分開(kāi)
foreach (string temp_Attributes in Attributes) //得到圖片地址屬性
if (temp_Attributes.IndexOf("src") >= 0)
{
imgSrc = temp_Attributes.ToString();
break;
}
imgSrc = imgSrc.Substring(imgSrc.IndexOf('"') + 1, imgSrc.LastIndexOf('"') - imgSrc.IndexOf('"') - 1); //叢地址屬性中提取地址
} while (imgSrc == "" && cat > 0);
return (imgSrc);
}
AspNet技術(shù):asp.net(c#)獲取內(nèi)容第一張圖片地址的函數(shù),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。