Skip to content

Commit

Permalink
OffScreen - CaptureScreenshotAsync change to Scale default
Browse files Browse the repository at this point in the history
- Previously when no Scale was specified, then the previous ChromiumWebBrowser scale was used.
- Now Viewport defaults to a scale of 1, so the behaviour has been changed so the scale passed as
  the param is always used.
- Test updated

This is a minor breaking change.

Resolves #4091
  • Loading branch information
amaitland committed May 5, 2022
1 parent 3ec3de7 commit f8b433f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
16 changes: 8 additions & 8 deletions CefSharp.OffScreen/ChromiumWebBrowser.cs
Expand Up @@ -550,6 +550,11 @@ public async Task<byte[]> CaptureScreenshotAsync(CaptureScreenshotFormat? format
return screenShot.Data;
}

if (viewport.Scale <= 0)
{
throw new ArgumentException($"{nameof(viewport)}.{nameof(viewport.Scale)} must be greater than 0.");
}

//https://bitbucket.org/chromiumembedded/cef/issues/3103/offscreen-capture-screenshot-with-devtools
//CEF OSR mode doesn't set the size internally when CaptureScreenShot is called with a clip param specified, so
//we must manually resize our view if size is greater
Expand All @@ -563,19 +568,14 @@ public async Task<byte[]> CaptureScreenshotAsync(CaptureScreenshotFormat? format
{
newHeight = size.Height;
}
var newScale = viewport.Scale;
if (newScale == 0)
{
newScale = deviceScaleFactor;
}

if ((int)newWidth > size.Width || (int)newHeight > size.Height || newScale != deviceScaleFactor)
if ((int)newWidth > size.Width || (int)newHeight > size.Height || viewport.Scale != deviceScaleFactor)
{
await ResizeAsync((int)newWidth, (int)newHeight, (float)newScale).ConfigureAwait(continueOnCapturedContext:false);
await ResizeAsync((int)newWidth, (int)newHeight, (float)viewport.Scale).ConfigureAwait(continueOnCapturedContext:false);
}

//Create a copy instead of modifying users object as we need to set Scale to 1
//as CEF doesn't support passing custom scale.
//as CEF doesn't support passing custom scale for OSR.
var clip = new Viewport
{
Height = viewport.Height,
Expand Down
2 changes: 1 addition & 1 deletion CefSharp.Test/OffScreen/OffScreenBrowserBasicFacts.cs
Expand Up @@ -808,7 +808,7 @@ public async Task CanCaptureScreenshotAsync()
}


var result3 = await browser.CaptureScreenshotAsync(viewport: new Viewport { Width = 100, Height = 200 });
var result3 = await browser.CaptureScreenshotAsync(viewport: new Viewport { Width = 100, Height = 200, Scale = 2 });
Assert.Equal(1466, browser.Size.Width);
Assert.Equal(968, browser.Size.Height);
Assert.Equal(2, browser.DeviceScaleFactor);
Expand Down
5 changes: 5 additions & 0 deletions CefSharp.WinForms/ChromiumWebBrowser.cs
Expand Up @@ -493,6 +493,11 @@ public async Task<byte[]> CaptureScreenshotAsync(CaptureScreenshotFormat format
ThrowExceptionIfDisposed();
ThrowExceptionIfBrowserNotInitialized();

if(viewPort != null && viewPort.Scale <= 0)
{
throw new ArgumentException($"{nameof(viewPort)}.{nameof(viewPort.Scale)} must be greater than 0.");
}

using (var devToolsClient = browser.GetDevToolsClient())
{
var screenShot = await devToolsClient.Page.CaptureScreenshotAsync(format, quality, viewPort, fromSurface, captureBeyondViewport).ConfigureAwait(continueOnCapturedContext: false);
Expand Down

0 comments on commit f8b433f

Please sign in to comment.