AngleSharp by Florian Rappl

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

.NET API 1,189,376 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; using System.Threading; using System.Threading.Tasks; namespace AngleSharp.Dom.Html { internal sealed class HtmlImageElement : HtmlElement, IHtmlImageElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IDisposable { private readonly BoundLocation _src; private CancellationTokenSource _cts; private Task<IImageInfo> _imageTask; public string ActualSource => Source; 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 _src.Href; } set { _src.Href = 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 (_imageTask == null) return 0; if (!_imageTask.IsCompleted || _imageTask.Result == null) return 0; return _imageTask.Result.Width; } } public int OriginalHeight { get { if (_imageTask == null) return 0; if (!_imageTask.IsCompleted || _imageTask.Result == null) return 0; return _imageTask.Result.Height; } } public bool IsCompleted { get { if (_imageTask != null) return _imageTask.IsCompleted; return true; } } public bool IsMap { get { return GetOwnAttribute(AttributeNames.IsMap) != null; } set { SetOwnAttribute(AttributeNames.IsMap, value ? string.Empty : null); } } public HtmlImageElement(Document owner, string prefix = null) : base(owner, Tags.Img, prefix, NodeFlags.SelfClosing | NodeFlags.Special) { _src = new BoundLocation(this, AttributeNames.Src); RegisterAttributeObserver(AttributeNames.Src, UpdateSource); } public void Dispose() { if (_cts != null) _cts.Cancel(); _cts = null; _imageTask = null; } private void UpdateSource(string value) { if (_cts != null) _cts.Cancel(); if (!string.IsNullOrEmpty(value)) { Url url = new Url(ActualSource); _cts = new CancellationTokenSource(); _imageTask = LoadAsync(url, _cts.Token); } } private async Task<IImageInfo> LoadAsync(Url url, CancellationToken cancel) { ResourceRequest request = this.CreateRequestFor(url); IImageInfo result = await AwaitExtensions.ConfigureAwait(Owner.LoadResource<IImageInfo>(request, cancel), false); this.FireSimpleEvent(EventNames.Load, false, false); return result; } } }