Skip to content

Commit 4dba519

Browse files
committed
OffScreen - CaptureScreenshotAsync only resize if larger and support clip
1 parent d179fe5 commit 4dba519

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ public Task<Bitmap> ScreenshotAsync(bool ignoreExistingScreenshot = false, Popup
533533
/// </summary>
534534
/// <param name="format">Image compression format (defaults to png).</param>
535535
/// <param name="quality">Compression quality from range [0..100] (jpeg only).</param>
536-
/// <param name="viewport">view port to capture, if not null the browser will be resized to match the width/height.</param>
536+
/// <param name="viewport">view port to capture, if not null the browser will be resized if the requested width/height
537+
/// are larger than the current browser <see cref="Size"/>.</param>
537538
/// <returns>A task that can be awaited to obtain the screenshot as a byte[].</returns>
538539
public async Task<byte[]> CaptureScreenshotAsync(CaptureScreenshotFormat? format = null, int? quality = null, Viewport viewport = null)
539540
{
@@ -542,15 +543,34 @@ public async Task<byte[]> CaptureScreenshotAsync(CaptureScreenshotFormat? format
542543

543544
using (var devToolsClient = browser.GetDevToolsClient())
544545
{
545-
if(viewport != null)
546+
if(viewport == null)
546547
{
547-
await ResizeAsync((int)viewport.Width, (int)viewport.Height, (float)viewport.Scale).ConfigureAwait(continueOnCapturedContext:false);
548+
var screenShot = await devToolsClient.Page.CaptureScreenshotAsync(format, quality, fromSurface: true).ConfigureAwait(continueOnCapturedContext: false);
549+
550+
return screenShot.Data;
548551
}
549552

553+
//TODO: Check scale
550554
//https://bitbucket.org/chromiumembedded/cef/issues/3103/offscreen-capture-screenshot-with-devtools
551555
//CEF OSR mode doesn't set the size internally when CaptureScreenShot is called with a clip param specified, so
552-
//we must manually resize our view.
553-
var response = await devToolsClient.Page.CaptureScreenshotAsync(format, quality, fromSurface:true).ConfigureAwait(continueOnCapturedContext: false);
556+
//we must manually resize our view if size is greater
557+
if (viewport.Scale > deviceScaleFactor || (int)viewport.Width > size.Width || (int)viewport.Height > size.Height)
558+
{
559+
await ResizeAsync((int)viewport.Width, (int)viewport.Height, (float)viewport.Scale).ConfigureAwait(continueOnCapturedContext:false);
560+
}
561+
562+
//Create a copy instead of modifying users object as we need to set Scale to 1
563+
//as CEF doesn't support passing custom scale.
564+
var clip = new Viewport
565+
{
566+
Height = viewport.Height,
567+
Width = viewport.Width,
568+
X = viewport.X,
569+
Y = viewport.Y,
570+
Scale = 1
571+
};
572+
573+
var response = await devToolsClient.Page.CaptureScreenshotAsync(format, quality, clip: clip, fromSurface: true).ConfigureAwait(continueOnCapturedContext: false);
554574

555575
return response.Data;
556576
}

0 commit comments

Comments
 (0)