Skip to content

Commit

Permalink
Use Desktop Duplication for Full Screen ScreenShot
Browse files Browse the repository at this point in the history
  • Loading branch information
MathewSachin committed Jul 30, 2017
1 parent 5860a9d commit cadfd61
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Captura.Core/Models/DeskDuplImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Captura.Models
public class DeskDuplImageProvider : IImageProvider
{
DesktopDuplicator _dupl;
int _monitor;
bool _includeCursor;
readonly int _monitor;
readonly bool _includeCursor;

public DeskDuplImageProvider(int Monitor, bool IncludeCursor)
{
Expand Down
15 changes: 14 additions & 1 deletion src/Captura.Core/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,20 @@ public async void CaptureScreenShot(string FileName = null)
await Task.Delay(100);
}

bmp = ScreenShot.Capture();
if (Settings.Instance.UseDeskDupl)
{
using (var dupl = new DesktopDuplication.DesktopDuplicator(WindowProvider.DesktopRectangle, includeCursor, 0))
{
// Increase timeout
dupl.Timeout = 10000;

// First one is blank
dupl.Capture().Dispose();

bmp = dupl.Capture();
}
}
else bmp = ScreenShot.Capture(includeCursor);

if (hide)
ServiceProvider.MainWindow.IsVisible = true;
Expand Down
25 changes: 11 additions & 14 deletions src/DesktopDuplication/DesktopDuplicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class DesktopDuplicator : IDisposable
readonly bool _includeCursor;
#endregion

public int Timeout { get; set; }

public DesktopDuplicator(DRectangle Rect, bool IncludeCursor, int Monitor, int Adapter = 0)
{
_rect = Rect;
Expand Down Expand Up @@ -86,13 +88,8 @@ public DesktopDuplicator(DRectangle Rect, bool IncludeCursor, int Monitor, int A
}
}
}

public void UpdateRectLocation(System.Drawing.Point P)
{
_rect.Location = P;
}

Bitmap lastFrame;

Bitmap _lastFrame;

public Bitmap Capture()
{
Expand All @@ -103,11 +100,11 @@ public Bitmap Capture()

try
{
_deskDupl.AcquireNextFrame(0, out _frameInfo, out desktopResource);
_deskDupl.AcquireNextFrame(Timeout, out _frameInfo, out desktopResource);
}
catch (SharpDXException e) when (e.ResultCode.Code == SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code)
{
return lastFrame ?? new Bitmap(_rect.Width, _rect.Height);
return _lastFrame ?? new Bitmap(_rect.Width, _rect.Height);
}
catch (SharpDXException e) when (e.ResultCode.Failure)
{
Expand Down Expand Up @@ -140,10 +137,10 @@ public Bitmap Capture()

Bitmap ProcessFrame(IntPtr SourcePtr, int SourceRowPitch)
{
lastFrame = new Bitmap(_rect.Width, _rect.Height, PixelFormat.Format32bppRgb);
_lastFrame = new Bitmap(_rect.Width, _rect.Height, PixelFormat.Format32bppRgb);

// Copy pixels from screen capture Texture to GDI bitmap
var mapDest = lastFrame.LockBits(new DRectangle(0, 0, _rect.Width, _rect.Height), ImageLockMode.WriteOnly, lastFrame.PixelFormat);
var mapDest = _lastFrame.LockBits(new DRectangle(0, 0, _rect.Width, _rect.Height), ImageLockMode.WriteOnly, _lastFrame.PixelFormat);

Parallel.For(0, _rect.Height, y =>
{
Expand All @@ -153,15 +150,15 @@ Bitmap ProcessFrame(IntPtr SourcePtr, int SourceRowPitch)
});

// Release source and dest locks
lastFrame.UnlockBits(mapDest);
_lastFrame.UnlockBits(mapDest);

if (_includeCursor && _frameInfo.PointerPosition.Visible)
{
using (var g = Graphics.FromImage(lastFrame))
using (var g = Graphics.FromImage(_lastFrame))
MouseCursor.Draw(g, P => new System.Drawing.Point(P.X - _rect.X, P.Y - _rect.Y));
}

return lastFrame;
return _lastFrame;
}

void ReleaseFrame()
Expand Down

0 comments on commit cadfd61

Please sign in to comment.