Skip to content

Commit f8b433f

Browse files
committed
OffScreen - CaptureScreenshotAsync change to Scale default
- 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
1 parent 3ec3de7 commit f8b433f

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,11 @@ public async Task<byte[]> CaptureScreenshotAsync(CaptureScreenshotFormat? format
550550
return screenShot.Data;
551551
}
552552

553+
if (viewport.Scale <= 0)
554+
{
555+
throw new ArgumentException($"{nameof(viewport)}.{nameof(viewport.Scale)} must be greater than 0.");
556+
}
557+
553558
//https://bitbucket.org/chromiumembedded/cef/issues/3103/offscreen-capture-screenshot-with-devtools
554559
//CEF OSR mode doesn't set the size internally when CaptureScreenShot is called with a clip param specified, so
555560
//we must manually resize our view if size is greater
@@ -563,19 +568,14 @@ public async Task<byte[]> CaptureScreenshotAsync(CaptureScreenshotFormat? format
563568
{
564569
newHeight = size.Height;
565570
}
566-
var newScale = viewport.Scale;
567-
if (newScale == 0)
568-
{
569-
newScale = deviceScaleFactor;
570-
}
571571

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

577577
//Create a copy instead of modifying users object as we need to set Scale to 1
578-
//as CEF doesn't support passing custom scale.
578+
//as CEF doesn't support passing custom scale for OSR.
579579
var clip = new Viewport
580580
{
581581
Height = viewport.Height,

CefSharp.Test/OffScreen/OffScreenBrowserBasicFacts.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ public async Task CanCaptureScreenshotAsync()
808808
}
809809

810810

811-
var result3 = await browser.CaptureScreenshotAsync(viewport: new Viewport { Width = 100, Height = 200 });
811+
var result3 = await browser.CaptureScreenshotAsync(viewport: new Viewport { Width = 100, Height = 200, Scale = 2 });
812812
Assert.Equal(1466, browser.Size.Width);
813813
Assert.Equal(968, browser.Size.Height);
814814
Assert.Equal(2, browser.DeviceScaleFactor);

CefSharp.WinForms/ChromiumWebBrowser.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,11 @@ public async Task<byte[]> CaptureScreenshotAsync(CaptureScreenshotFormat format
493493
ThrowExceptionIfDisposed();
494494
ThrowExceptionIfBrowserNotInitialized();
495495

496+
if(viewPort != null && viewPort.Scale <= 0)
497+
{
498+
throw new ArgumentException($"{nameof(viewPort)}.{nameof(viewPort.Scale)} must be greater than 0.");
499+
}
500+
496501
using (var devToolsClient = browser.GetDevToolsClient())
497502
{
498503
var screenShot = await devToolsClient.Page.CaptureScreenshotAsync(format, quality, viewPort, fromSurface, captureBeyondViewport).ConfigureAwait(continueOnCapturedContext: false);

0 commit comments

Comments
 (0)