AngleSharp by Florian Rappl

<PackageReference Include="AngleSharp" Version="0.9.1" />

.NET API 1,208,832 bytes

 HtmlImageElement

Represents the image element.
using AngleSharp.Dom.Css; using AngleSharp.Extensions; using AngleSharp.Html; using AngleSharp.Network; using AngleSharp.Services.Media; using System.Threading.Tasks; namespace AngleSharp.Dom.Html { internal sealed class HtmlImageElement : HtmlElement, IHtmlImageElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle { private Url _lastSource; private IImageInfo _img; public string ActualSource { get { if (_lastSource == null) return null; return _lastSource.Href; } } public string SourceSet { get { return GetOwnAttribute(AttributeNames.SrcSet); } set { SetOwnAttribute(AttributeNames.SrcSet, value); } } public string Sizes { get { return GetOwnAttribute(AttributeNames.Sizes); } set { SetOwnAttribute(AttributeNames.Sizes, value); } } public string Source { get { return GetUrlAttribute(AttributeNames.Src); } set { SetOwnAttribute(AttributeNames.Src, value); } } public string AlternativeText { get { return GetOwnAttribute(AttributeNames.Alt); } set { SetOwnAttribute(AttributeNames.Alt, value); } } public string CrossOrigin { get { return GetOwnAttribute(AttributeNames.CrossOrigin); } set { SetOwnAttribute(AttributeNames.CrossOrigin, value); } } public string UseMap { get { return GetOwnAttribute(AttributeNames.UseMap); } set { SetOwnAttribute(AttributeNames.UseMap, value); } } public int DisplayWidth { get { return GetOwnAttribute(AttributeNames.Width).ToInteger(OriginalWidth); } set { SetOwnAttribute(AttributeNames.Width, value.ToString()); } } public int DisplayHeight { get { return GetOwnAttribute(AttributeNames.Height).ToInteger(OriginalHeight); } set { SetOwnAttribute(AttributeNames.Height, value.ToString()); } } public int OriginalWidth { get { if (!IsCompleted) return 0; return _img.Width; } } public int OriginalHeight { get { if (!IsCompleted) return 0; return _img.Height; } } public bool IsCompleted => _img != null; public bool IsMap { get { return HasOwnAttribute(AttributeNames.IsMap); } set { SetOwnAttribute(AttributeNames.IsMap, value ? string.Empty : null); } } public HtmlImageElement(Document owner, string prefix = null) : base(owner, Tags.Img, prefix, NodeFlags.SelfClosing | NodeFlags.Special) { RegisterAttributeObserver(AttributeNames.Src, UpdateSource); RegisterAttributeObserver(AttributeNames.SrcSet, UpdateSource); RegisterAttributeObserver(AttributeNames.Sizes, UpdateSource); RegisterAttributeObserver(AttributeNames.CrossOrigin, UpdateSource); } private void GetImage(Url source) { if (source.IsInvalid) source = null; else if (_lastSource != null && source.Equals(_lastSource)) { return; } this.CancelTasks(); _lastSource = source; if (source != null) { ResourceRequest request = this.CreateRequestFor(source); this.LoadResource<IImageInfo>(request).ContinueWith(delegate(Task<IImageInfo> m) { if (!m.IsFaulted) _img = m.Result; this.FireLoadOrErrorEvent(m); }); } } private void UpdateSource(string value) { GetImage(this.GetImageCandidate()); } } }