HtmlDocument.Images 屬性

定義

取得文件中所有影像標記的集合。

public:
 property System::Windows::Forms::HtmlElementCollection ^ Images { System::Windows::Forms::HtmlElementCollection ^ get(); };
public System.Windows.Forms.HtmlElementCollection Images { get; }
member this.Images : System.Windows.Forms.HtmlElementCollection
Public ReadOnly Property Images As HtmlElementCollection

屬性值

HtmlElementCollection

HtmlElement 物件的集合,文件中的每個 IMG 標記各一個物件。 會根據來源順序,從集合中傳回項目。

範例

下列程式碼範例會 ALT 檢查檔中所有影像的 屬性,如果尚未設定值,則會設定預設 ALT 屬性。

private string[] GetImageUrls()
{
    if (webBrowser1.Document != null)
    {
        HtmlDocument doc = webBrowser1.Document;
        string[] urls = (string[])Array.CreateInstance(Type.GetType("System.String"), doc.Images.Count);

        foreach (HtmlElement imgElement in doc.Images)
        {
            urls[urls.Length] = imgElement.GetAttribute("src");
        }
        return (urls);
    }
    else
    {
        return (new string[0]);
    }
}
Private Function GetImageUrls() As String()
    If (WebBrowser1.Document IsNot Nothing) Then
        Dim Urls(WebBrowser1.Document.Images.Count) As String

        For Each ImgElement As HtmlElement In WebBrowser1.Document.Images
            Urls(Urls.Length) = ImgElement.GetAttribute("SRC")
        Next

        GetImageUrls = Urls
    Else
        Dim Urls(0) As String
        GetImageUrls = Urls
    End If
End Function

備註

Images 會傳回 物件的集合 HtmlElement 。 若要存取 未由 直接公開 HtmlElement 的屬性,例如 ALTSRC ,請使用 GetAttribute 方法。

若要將新影像新增至檔,請建立新的 IMG 標記做為字串,並將它指派給 InnerHtml 先前新增至 HTML DOM 的專案屬性;或使用 方法,使用 CreateElement 設定其屬性 SetAttribute ,然後使用 將它新增為現有元素 AppendChild 的子系。

適用於

另請參閱