@@ -533,7 +533,8 @@ public Task<Bitmap> ScreenshotAsync(bool ignoreExistingScreenshot = false, Popup
533
533
/// </summary>
534
534
/// <param name="format">Image compression format (defaults to png).</param>
535
535
/// <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>
537
538
/// <returns>A task that can be awaited to obtain the screenshot as a byte[].</returns>
538
539
public async Task < byte [ ] > CaptureScreenshotAsync ( CaptureScreenshotFormat ? format = null , int ? quality = null , Viewport viewport = null )
539
540
{
@@ -542,15 +543,34 @@ public async Task<byte[]> CaptureScreenshotAsync(CaptureScreenshotFormat? format
542
543
543
544
using ( var devToolsClient = browser . GetDevToolsClient ( ) )
544
545
{
545
- if ( viewport ! = null )
546
+ if ( viewport = = null )
546
547
{
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 ;
548
551
}
549
552
553
+ //TODO: Check scale
550
554
//https://bitbucket.org/chromiumembedded/cef/issues/3103/offscreen-capture-screenshot-with-devtools
551
555
//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 ) ;
554
574
555
575
return response . Data ;
556
576
}
0 commit comments