/// <summary> /// Asynchronously returns an <see cref="Image"/> that represents a screenshot /// of the specified <see cref="Control"/> as it appears on the browser. /// </summary> /// <param name="target"> /// The <see cref="Control"/> to render to an <see cref="Image"/> /// </param> /// <param name="options">The <see cref="Html2CanvasOptions"/> to pass to the html2canvas call.</param> /// <returns>An awaitable <see cref="Task"/> that contains the screenshot.</returns> public static Task <Image> ScreenshotAsync(Control target, Html2CanvasOptions options = null) { if (target == null) { throw new ArgumentNullException(nameof(target)); } var tcs = new TaskCompletionSource <Image>(); Instance.ScreenshotCore(target, options, (result) => { if (result is Exception) { tcs.SetException((Exception)result); } else if (result is Image) { tcs.SetResult((Image)result); } else { tcs.SetResult(null); } }); return(tcs.Task); }
/// <summary> /// Takes a screenshot of the specified <see cref="Control"/>. /// </summary> /// <param name="target"> /// The <see cref="Control"/> to render to an <see cref="Image"/> /// </param> /// <param name="callback">Callback method that receives the <see cref="Image"/> object.</param> /// <param name="options">The <see cref="Html2CanvasOptions"/> to pass to the html2canvas call.</param> /// <exception cref="ArgumentNullException">If any of the arguments is null.</exception> public static void Screenshot(Control target, Html2CanvasOptions options, Action <Image> callback) { if (target == null) { throw new ArgumentNullException(nameof(target)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } if (callback == null) { throw new ArgumentNullException(nameof(callback)); } Instance.ScreenshotCore(target, options, (result) => { if (result is Exception) { throw (Exception)result; } else { callback(result as Image); } }); }
/// <summary> /// Asynchronously returns an <see cref="Image"/> that represents a screenshot /// of the browser. /// </summary> /// <param name="options">The <see cref="Html2CanvasOptions"/> to pass to the html2canvas call.</param> /// <returns>An awaitable <see cref="Task"/> that contains the screenshot.</returns> public static Task <Image> ScreenshotAsync(Html2CanvasOptions options = null) { var tcs = new TaskCompletionSource <Image>(); Instance.ScreenshotCore(null, options, (image) => { tcs.SetResult(image); }); return(tcs.Task); }
/// <summary> /// Takes a screenshot of the browser. /// </summary> /// <param name="options">The <see cref="Html2CanvasOptions"/> to pass to the html2canvas call.</param> /// <param name="callback">Callback method that receives the <see cref="Image"/> object.</param> /// <exception cref="ArgumentNullException">If any of the arguments is null.</exception> public static void Screenshot(Html2CanvasOptions options, Action <Image> callback) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (callback == null) { throw new ArgumentNullException(nameof(callback)); } Instance.ScreenshotCore(null, options, callback); }
// Implementation private void ScreenshotCore(Control target, Html2CanvasOptions options, Action <object> callback) { Call("screenshot", (result) => { if (result is string) { result = ImageFromBase64((string)result); } callback(result); }, new object[] { target, options }); }
/// <summary> /// Asynchronously returns an <see cref="Image"/> that represents a screenshot /// of the browser. /// </summary> /// <param name="options">The <see cref="Html2CanvasOptions"/> to pass to the html2canvas call.</param> /// <returns>An awaitable <see cref="Task"/> that contains the screenshot.</returns> public static Task <Image> ScreenshotAsync(Html2CanvasOptions options = null) { var tcs = new TaskCompletionSource <Image>(); Instance.ScreenshotCore(null, options, (result) => { if (result is Exception) { tcs.SetException((Exception)result); } else { tcs.SetResult(result as Image); } }); return(tcs.Task); }
// Implementation private void ScreenshotCore(Control target, Html2CanvasOptions options, Action <Image> callback) { var handle = (IntPtr)GCHandle.Alloc(callback, GCHandleType.Weak); Call("screenshot", target, options, handle.ToInt64()); }