Skip to content

Commit

Permalink
Example - Update CaptureScreenShotAsPng to use strongly typed DevTool…
Browse files Browse the repository at this point in the history
…s client
  • Loading branch information
amaitland committed Aug 28, 2021
1 parent 898b843 commit 46e9a1b
Showing 1 changed file with 5 additions and 34 deletions.
39 changes: 5 additions & 34 deletions CefSharp.Example/DevTools/DevToolsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace CefSharp.Example.DevTools
{
Expand All @@ -15,42 +14,14 @@ public static class DevToolsExtensions
/// <returns>png encoded image as byte[]</returns>
public static async Task<byte[]> CaptureScreenShotAsPng(this IWebBrowser chromiumWebBrowser)
{
//if (!browser.HasDocument)
//{
// throw new System.Exception("Page hasn't loaded");
//}

var browser = chromiumWebBrowser.GetBrowser();

if (browser == null || browser.IsDisposed)
{
throw new Exception("browser is Null or Disposed");
}

//var param = new Dictionary<string, object>
//{
// { "format", "png" },
//}

//Make sure to dispose of our observer registration when done
using (var devToolsClient = browser.GetDevToolsClient())
//If you need to make multiple calls then reuse the devtools client
//and Dispose when done.
using (var devToolsClient = chromiumWebBrowser.GetDevToolsClient())
{
const string methodName = "Page.captureScreenshot";

var result = await devToolsClient.ExecuteDevToolsMethodAsync(methodName);

dynamic response = JsonConvert.DeserializeObject<dynamic>(result.ResponseAsJsonString);

//Success
if (result.Success)
{
return Convert.FromBase64String((string)response.data);
}

var code = (string)response.code;
var message = (string)response.message;
var result = await devToolsClient.Page.CaptureScreenshotAsync();

throw new Exception(code + ":" + message);
return result.Data;
}
}
}
Expand Down

0 comments on commit 46e9a1b

Please sign in to comment.