Skip to main content

Html2Canvas

Namespace: Wisej.Web.Ext.Html2Canvas

Assembly: Wisej.Web.Ext.Html2Canvas (4.1.0.0)

Implementation of the html2canvas (https://html2canvas.hertzen.com/) library. It's a singleton instance that can take a screenshot of a specific control or the entire browser and send the image back to the server.

public class Html2Canvas : Component

Use the static methods directly, the session instance is created automatically:

Html2Canvas.Screenshot(this, (image) => {
image.Save(@"\images\screen.png");
});

// or using asynchronous code:

var image = await Html2Canvas.ScreenshotAsync(this.panel1);
image.Save(@"\images\screen.png");

Methods​

Protected member OnWebRender(config)​

Renders the client component.

ParameterTypeDescription
configObjectDynamic configuration object.

Static member Screenshot(callback)​

Takes a screenshot of the browser.

ParameterTypeDescription
callbackAction<Image>Callback method that receives the Image object, or null if the image couldn't be decoded.

The screenshot is rendered asynchronously in the browser by the html2canvas library (https://html2canvas.hertzen.com/), which re-creates the image from the DOM and the CSS styles; it's not an actual screen capture, so some content (i.e. cross-origin images or iframes) may not be rendered. The image is returned as a PNG Bitmap.

The method returns immediately and callback is invoked when the image is received from the browser. If the client reports an error, the exception is thrown when the response is processed, not by this method.

Throws:

Example:

Saving a screenshot of the whole browser page:

private void buttonCapture_Click(object sender, EventArgs e)
{
Html2Canvas.Screenshot(image =>
{
image?.Save(Path.Combine(Application.StartupPath, "Screenshots", "page.png"));
});
}

Static member Screenshot(options, callback)​

Takes a screenshot of the browser using the specified options .

ParameterTypeDescription
optionsHtml2CanvasOptionsThe Html2CanvasOptions to pass to the html2canvas call.
callbackAction<Image>Callback method that receives the Image object, or null if the image couldn't be decoded.

The screenshot is rendered asynchronously in the browser by the html2canvas library (https://html2canvas.hertzen.com/), which re-creates the image from the DOM and the CSS styles; it's not an actual screen capture, so some content (i.e. cross-origin images or iframes) may not be rendered. The image is returned as a PNG Bitmap.

The method returns immediately and callback is invoked when the image is received from the browser. If the client reports an error, the exception is thrown when the response is processed, not by this method.

Throws:

Example:

Taking a screenshot with a white background at the original size:

var options = new Html2CanvasOptions
{
BackgroundColor = Color.White,
Scale = 1
};

Html2Canvas.Screenshot(options, image =>
{
this.pictureBox1.Image = image;
});

Static member Screenshot(target, callback)​

Takes a screenshot of the specified Control.

ParameterTypeDescription
targetControlThe Control to render to an Image.
callbackAction<Image>Callback method that receives the Image object, or null if the image couldn't be decoded.

The screenshot is rendered asynchronously in the browser by the html2canvas library (https://html2canvas.hertzen.com/), which re-creates the image from the DOM and the CSS styles; it's not an actual screen capture, so some content (i.e. cross-origin images or iframes) may not be rendered. The image is returned as a PNG Bitmap.

The method returns immediately and callback is invoked when the image is received from the browser. If the client reports an error, the exception is thrown when the response is processed, not by this method.

The target control must be created and visible on the client.

Throws:

Example:

Displaying a snapshot of a panel in a picture box:

private void buttonCapture_Click(object sender, EventArgs e)
{
Html2Canvas.Screenshot(this.panelChart, image =>
{
this.pictureBoxPreview.Image = image;
});
}

Static member Screenshot(target, options, callback)​

Takes a screenshot of the specified Control using the specified options .

ParameterTypeDescription
targetControlThe Control to render to an Image.
optionsHtml2CanvasOptionsThe Html2CanvasOptions to pass to the html2canvas call.
callbackAction<Image>Callback method that receives the Image object, or null if the image couldn't be decoded.

The screenshot is rendered asynchronously in the browser by the html2canvas library (https://html2canvas.hertzen.com/), which re-creates the image from the DOM and the CSS styles; it's not an actual screen capture, so some content (i.e. cross-origin images or iframes) may not be rendered. The image is returned as a PNG Bitmap.

The method returns immediately and callback is invoked when the image is received from the browser. If the client reports an error, the exception is thrown when the response is processed, not by this method.

The target control must be created and visible on the client.

Throws:

Example:

Capturing a form at double resolution:

Html2Canvas.Screenshot(this, new Html2CanvasOptions { BackgroundColor = Color.White, Scale = 2 }, image =>
{
image?.Save(Path.Combine(Application.StartupPath, "Screenshots", "form.png"));
});

Static member ScreenshotAsync(options)​

Asynchronously returns an Image that represents a screenshot of the browser.

ParameterTypeDescription
options optionalHtml2CanvasOptionsThe Html2CanvasOptions to pass to the html2canvas call, or null to use the defaults.

Returns: Task<Image>. An awaitable Task that contains the screenshot, or null if the image couldn't be decoded.

The screenshot is rendered in the browser by the html2canvas library (https://html2canvas.hertzen.com/), which re-creates the image from the DOM and the CSS styles; it's not an actual screen capture, so some content (i.e. cross-origin images or iframes) may not be rendered. The image is returned as a PNG Bitmap. If the client reports an error, the returned task is faulted with the exception.

Example:

Attaching a screenshot of the page to a support request:

private async void buttonReport_Click(object sender, EventArgs e)
{
var image = await Html2Canvas.ScreenshotAsync();
if (image != null)
SendSupportRequest(this.textBoxDescription.Text, image);
}

Static member ScreenshotAsync(target, options)​

Asynchronously returns an Image that represents a screenshot of the specified Control as it appears on the browser.

ParameterTypeDescription
targetControlThe Control to render to an Image.
options optionalHtml2CanvasOptionsThe Html2CanvasOptions to pass to the html2canvas call, or null to use the defaults.

Returns: Task<Image>. An awaitable Task that contains the screenshot, or null if the image couldn't be decoded.

The screenshot is rendered in the browser by the html2canvas library (https://html2canvas.hertzen.com/), which re-creates the image from the DOM and the CSS styles; it's not an actual screen capture, so some content (i.e. cross-origin images or iframes) may not be rendered. The image is returned as a PNG Bitmap. If the client reports an error, the returned task is faulted with the exception. The target control must be created and visible on the client.

Throws:

Example:

Capturing a panel with await:

private async void buttonCapture_Click(object sender, EventArgs e)
{
var image = await Html2Canvas.ScreenshotAsync(this.panelDashboard, new Html2CanvasOptions { BackgroundColor = Color.White, Scale = 1 });
this.pictureBox1.Image = image;
}

Implements​