HtmlCanvasElement Class

https://html.spec.whatwg.org/multipage/canvas.html#htmlcanvaselement

public sealed class HtmlCanvasElement : HtmlElement
Inheritance
HtmlElement HtmlCanvasElement

Properties

NameDescription
Heighthttps://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-height
Widthhttps://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-width

https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-height

public uint Height { get; set; }

Property value

uint

Exceptions

DomException
An InvalidStateError when the canvas is the placeholder of an OffscreenCanvas.

See also

https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-width

public uint Width { get; set; }

Property value

uint

Exceptions

DomException
An InvalidStateError when the canvas is the placeholder of an OffscreenCanvas.

See also

Methods

NameDescription
GetContext(string, CanvasRenderingContext2DSettings?)Returns the rendering context of the canvas (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-getcontext): a CanvasRenderingContext2D for "2d" and an ImageBitmapRenderingContext for "bitmaprenderer" (created the first time), or null for a context type that is not supported ("webgl", "webgl2", "webgpu") or that does not match the context of the canvas. The 2D context needs a rendering backend (BrowsingContextOptions.RenderingBackend): without one, getContext("2d") returns null.
ToBlob(BlobCallback, string, double?)Serializes the bitmap as an image file (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-toblob) and invokes the callback with a Blob of the file in a task (null when the canvas has no pixels).
ToDataUrl(string, double?)Returns a data: URL of the bitmap serialized as an image file (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-todataurl): PNG, JPEG or WebP (another type is serialized as PNG), or data:, when the canvas has no pixels.
TransferControlToOffscreen()Transfers the control of the canvas to an OffscreenCanvas (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-transfercontroltooffscreen): the canvas becomes its placeholder and displays the frames drawn by its rendering context (e.g. in a worker).

GetContext(string, CanvasRenderingContext2DSettings?)

EmbeddedContent.cs

Returns the rendering context of the canvas (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-getcontext): a CanvasRenderingContext2D for "2d" and an ImageBitmapRenderingContext for "bitmaprenderer" (created the first time), or null for a context type that is not supported ("webgl", "webgl2", "webgpu") or that does not match the context of the canvas. The 2D context needs a rendering backend (BrowsingContextOptions.RenderingBackend): without one, getContext("2d") returns null.

public object? GetContext(string contextId, CanvasRenderingContext2DSettings? options = null)

Parameters

contextId string
The type of the context.
options CanvasRenderingContext2DSettings?
The options of the context.

Returns

object? The context, or null.

Exceptions

DomException
An InvalidStateError when the canvas is the placeholder of an OffscreenCanvas.

See also

ToBlob(BlobCallback, string, double?)

EmbeddedContent.cs

Serializes the bitmap as an image file (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-toblob) and invokes the callback with a Blob of the file in a task (null when the canvas has no pixels).

public void ToBlob(BlobCallback callback, string type = "image/png", double? quality = null)

Parameters

callback BlobCallback
The callback.
type string
The MIME type of the file (PNG, JPEG or WebP; another type is serialized as PNG).
quality double?
The quality of the lossy formats: a number from 0 to 1, else the default quality.

Exceptions

DomException
An InvalidStateError for the placeholder of an OffscreenCanvas, a SecurityError when the canvas is not origin-clean.

See also

ToDataUrl(string, double?)

EmbeddedContent.cs

Returns a data: URL of the bitmap serialized as an image file (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-todataurl): PNG, JPEG or WebP (another type is serialized as PNG), or data:, when the canvas has no pixels.

public string ToDataUrl(string type = "image/png", double? quality = null)

Parameters

type string
The MIME type of the file.
quality double?
The quality of the lossy formats from 0 to 1 (another value is the default quality), or null for the default quality.

Returns

string

Exceptions

DomException
A SecurityError when the canvas is not origin-clean.

See also

TransferControlToOffscreen()

EmbeddedContent.cs

Transfers the control of the canvas to an OffscreenCanvas (https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-transfercontroltooffscreen): the canvas becomes its placeholder and displays the frames drawn by its rendering context (e.g. in a worker).

public OffscreenCanvas TransferControlToOffscreen()

Returns

OffscreenCanvas

Exceptions

DomException
An InvalidStateError when the canvas has a context or was already transferred.

See also