ImagingScreenshotExtensions Class

Takes screenshots of the documents of a browsing context: the viewport of a browsing context or of a document, or the area of an element (its border box, as getBoundingClientRect()), at the device pixel ratio of the viewport.

public static class ImagingScreenshotExtensions

Remarks

A screenshot paints the current state of the document: the layout is computed when it is out of date (as by the geometry APIs), so call it from the thread of the DomEventLoop of the browsing context, after the resources you expect are loaded (e.g. after DomEventLoop.RunUntilIdleAsync). The pixels are in sRGB, not premultiplied by alpha: a top-level document is painted over the color of its canvas (white, or a dark color for a dark color scheme) unless ScreenshotOptions.OmitBackground is set, and the parts that nothing paints (e.g. a nested document, which has no canvas color) are transparent.

The ImagingRenderingBackend of the browsing context (BrowsingContextOptions.RenderingBackend) rasterizes the screenshot. Without one, a temporary backend with the default options is used, but the images are not drawn: their encoded bytes are only kept by the documents of a browsing context that has a rendering backend.

Methods

NameDescription
TakeScreenshot(BrowsingContext, ScreenshotOptions?)Renders the viewport of the active document of a browsing context.
TakeScreenshot(Document, ScreenshotOptions?)Renders the viewport of a document (the area of its iframe for the document of a nested browsing context).
TakeScreenshot(Element, ScreenshotOptions?)Renders the area of an element: its border box in the viewport (the rectangle of getBoundingClientRect(), which includes its transforms), with what is painted over it. The parts of the element outside of the viewport are painted too.
TakeScreenshotAsJpeg(BrowsingContext, ScreenshotOptions?)Renders the viewport of the active document of a browsing context, encoded as JPEG (composited over white).
TakeScreenshotAsJpeg(Document, ScreenshotOptions?)Renders the viewport of a document, encoded as JPEG (composited over white).
TakeScreenshotAsJpeg(Element, ScreenshotOptions?)Renders the area of an element (see ImagingScreenshotExtensions.TakeScreenshot), encoded as JPEG (composited over white).
TakeScreenshotAsPng(BrowsingContext, ScreenshotOptions?)Renders the viewport of the active document of a browsing context, encoded as PNG.
TakeScreenshotAsPng(Document, ScreenshotOptions?)Renders the viewport of a document, encoded as PNG.
TakeScreenshotAsPng(Element, ScreenshotOptions?)Renders the area of an element (see ImagingScreenshotExtensions.TakeScreenshot), encoded as PNG.

TakeScreenshot overloads

TakeScreenshot(BrowsingContext, ScreenshotOptions?)

ImagingScreenshotExtensions.cs

Renders the viewport of the active document of a browsing context.

public static Image<Rgba32> TakeScreenshot(this BrowsingContext browsingContext, ScreenshotOptions? options = null)
Parameters
browsingContext BrowsingContext
The browsing context.
options ScreenshotOptions?
The options, or null for the default options.
Returns

Image<Rgba32> An image of the size of the viewport in device pixels. The caller owns it.

Exceptions
InvalidOperationException
The document is not rendered.

TakeScreenshot(Document, ScreenshotOptions?)

ImagingScreenshotExtensions.cs

Renders the viewport of a document (the area of its iframe for the document of a nested browsing context).

public static Image<Rgba32> TakeScreenshot(this Document document, ScreenshotOptions? options = null)
Parameters
document Document
The document, which must be the active document of a browsing context.
options ScreenshotOptions?
The options, or null for the default options.
Returns

Image<Rgba32> An image of the size of the viewport in device pixels. The caller owns it.

Exceptions
InvalidOperationException
The document is not rendered (it has no browsing context).

TakeScreenshot(Element, ScreenshotOptions?)

ImagingScreenshotExtensions.cs

Renders the area of an element: its border box in the viewport (the rectangle of getBoundingClientRect(), which includes its transforms), with what is painted over it. The parts of the element outside of the viewport are painted too.

public static Image<Rgba32> TakeScreenshot(this Element element, ScreenshotOptions? options = null)
Parameters
element Element
The element.
options ScreenshotOptions?
The options, or null for the default options.
Returns

Image<Rgba32> An image of the size of the border box of the element in device pixels (rounded outwards). The caller owns it.

Exceptions
InvalidOperationException
The document of the element is not rendered, or the element has no box or an empty box.

TakeScreenshotAsJpeg overloads

TakeScreenshotAsJpeg(BrowsingContext, ScreenshotOptions?)

ImagingScreenshotExtensions.cs

Renders the viewport of the active document of a browsing context, encoded as JPEG (composited over white).

public static byte[] TakeScreenshotAsJpeg(this BrowsingContext browsingContext, ScreenshotOptions? options = null)
Parameters
browsingContext BrowsingContext
options ScreenshotOptions?
Returns

byte[] The bytes of the JPEG image, of the quality of ScreenshotOptions.Quality.

Exceptions
InvalidOperationException
The document is not rendered.

TakeScreenshotAsJpeg(Document, ScreenshotOptions?)

ImagingScreenshotExtensions.cs

Renders the viewport of a document, encoded as JPEG (composited over white).

public static byte[] TakeScreenshotAsJpeg(this Document document, ScreenshotOptions? options = null)
Parameters
document Document
options ScreenshotOptions?
Returns

byte[] The bytes of the JPEG image, of the quality of ScreenshotOptions.Quality.

Exceptions
InvalidOperationException
The document is not rendered (it has no browsing context).

TakeScreenshotAsJpeg(Element, ScreenshotOptions?)

ImagingScreenshotExtensions.cs

Renders the area of an element (see ImagingScreenshotExtensions.TakeScreenshot), encoded as JPEG (composited over white).

public static byte[] TakeScreenshotAsJpeg(this Element element, ScreenshotOptions? options = null)
Parameters
element Element
options ScreenshotOptions?
Returns

byte[] The bytes of the JPEG image, of the quality of ScreenshotOptions.Quality.

Exceptions
InvalidOperationException
The document of the element is not rendered, or the element has no box or an empty box.

TakeScreenshotAsPng overloads

TakeScreenshotAsPng(BrowsingContext, ScreenshotOptions?)

ImagingScreenshotExtensions.cs

Renders the viewport of the active document of a browsing context, encoded as PNG.

public static byte[] TakeScreenshotAsPng(this BrowsingContext browsingContext, ScreenshotOptions? options = null)
Parameters
browsingContext BrowsingContext
options ScreenshotOptions?
Returns

byte[] The bytes of the PNG image.

Exceptions
InvalidOperationException
The document is not rendered.

TakeScreenshotAsPng(Document, ScreenshotOptions?)

ImagingScreenshotExtensions.cs

Renders the viewport of a document, encoded as PNG.

public static byte[] TakeScreenshotAsPng(this Document document, ScreenshotOptions? options = null)
Parameters
document Document
options ScreenshotOptions?
Returns

byte[] The bytes of the PNG image.

Exceptions
InvalidOperationException
The document is not rendered (it has no browsing context).

TakeScreenshotAsPng(Element, ScreenshotOptions?)

ImagingScreenshotExtensions.cs

Renders the area of an element (see ImagingScreenshotExtensions.TakeScreenshot), encoded as PNG.

public static byte[] TakeScreenshotAsPng(this Element element, ScreenshotOptions? options = null)
Parameters
element Element
options ScreenshotOptions?
Returns

byte[] The bytes of the PNG image.

Exceptions
InvalidOperationException
The document of the element is not rendered, or the element has no box or an empty box.