您需要對圖像執行請求,然後將其保存在您的機器上。 首先獲得圖像的URI:
var html = @"<img style=""background:Url('..//contactdetails?data=4512354367432554')"" src=""some transparent image""/>";
var regex = new Regex(@"//(?<Path>[^']+)'", RegexOptions.Singleline)
var uri = regex.Match(html).Groups["Path"].Value;
編輯
如果您正在使用HtmlAgilityPack
(因爲你已經提取的a
標籤),您可以使用Attributes
收集得到style
屬性,對正則表達式進行匹配,或者您可以直接使用OuterHtml
屬性來匹配這樣的模式:
var anchorTag = YourCodeToGetTheAnchorTag();
var attribute = anchorTag.Attributes["style"];
var match = regex.Match(attribute.Value);
var uri = match.Groups["Path"].Value;
或者使用OuterHtml
屬性:
var anchorTag = YourCodeToGetTheAnchorTag();
var match = regex.Match(anchorTag.OuterHtml);
var uri = match.Groups["Path"].Value
接下來,拼接圖像的URI從服務器的目錄,並創建一個請求:
var fullUri = "http://www.example.com/" + uri;
var request = (HttpWebRequest)WebRequest.Create(fullUri);
得到響應並保存圖像:
var response = request.GetResponse();
var image = Image.FromStream(response.GetResponseStream());
image.Save("path-on-your-machine");
謝謝RePierre,我使用htmlagilitypack來提取。你可以展示如何在C#中做到這一點。我嘗試修改你的代碼並使用,但它不工作。 – Maddy 2012-07-20 12:23:40
@ user1516690,請參閱編輯;希望能幫助到你。 – RePierre 2012-07-24 06:24:07