HtmlDocument.Images Property

Definition

Gets a collection of all image tags in the document.

C#
public System.Windows.Forms.HtmlElementCollection Images { get; }

Property Value

A collection of HtmlElement objects, one for each IMG tag in the document. Elements are returned from the collection in source order.

Examples

The following code example examines the ALT attribute of all of the images in the document, and sets a default ALT attribute if a value is not already set.

C#
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]);
    }
}

Remarks

Images returns a collection of HtmlElement objects. To access attributes, such as ALT and SRC, that are not directly exposed by HtmlElement, use the GetAttribute method.

To add a new image to a document, either create a new IMG tag as a string, and assign it to the InnerHtml property of an element previously added to the HTML DOM; or use the CreateElement method, set its properties using SetAttribute, and add it as a child of an existing element using AppendChild.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also