Skip to content

Commit 19080e6

Browse files
committed
Small refactorings
1 parent dee0c09 commit 19080e6

File tree

10 files changed

+940
-950
lines changed

10 files changed

+940
-950
lines changed

ScreenCapture.NET/DirectX/DX11ScreenCapture.cs

Lines changed: 320 additions & 321 deletions
Large diffs are not rendered by default.

ScreenCapture.NET/DirectX/DX11ScreenCaptureService.cs

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,82 @@
22
using System.Collections.Generic;
33
using Vortice.DXGI;
44

5-
namespace ScreenCapture.NET
5+
namespace ScreenCapture.NET;
6+
7+
/// <summary>
8+
/// Represents a <see cref="IScreenCaptureService"/> using the <see cref="DX11ScreenCapture"/>.
9+
/// </summary>
10+
public class DX11ScreenCaptureService : IScreenCaptureService
611
{
7-
/// <summary>
8-
///
9-
/// </summary>
10-
public class DX11ScreenCaptureService : IScreenCaptureService
11-
{
12-
#region Properties & Fields
12+
#region Properties & Fields
1313

14-
private readonly IDXGIFactory1 _factory;
14+
private readonly IDXGIFactory1 _factory;
1515

16-
private readonly Dictionary<Display, DX11ScreenCapture> _screenCaptures = new();
16+
private readonly Dictionary<Display, DX11ScreenCapture> _screenCaptures = new();
1717

18-
#endregion
18+
#endregion
1919

20-
#region Constructors
20+
#region Constructors
2121

22-
/// <summary>
23-
///
24-
/// </summary>
25-
public DX11ScreenCaptureService()
26-
{
27-
DXGI.CreateDXGIFactory1(out _factory).CheckError();
28-
}
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="DX11ScreenCaptureService"/> class.
24+
/// </summary>
25+
public DX11ScreenCaptureService()
26+
{
27+
DXGI.CreateDXGIFactory1(out _factory!).CheckError();
28+
}
2929

30-
#endregion
30+
#endregion
3131

32-
#region Methods
32+
#region Methods
3333

34-
/// <inheritdoc />
35-
public IEnumerable<GraphicsCard> GetGraphicsCards()
34+
/// <inheritdoc />
35+
public IEnumerable<GraphicsCard> GetGraphicsCards()
36+
{
37+
int i = 0;
38+
while (_factory.EnumAdapters1(i, out IDXGIAdapter1 adapter).Success)
3639
{
37-
int i = 0;
38-
while (_factory.EnumAdapters1(i, out IDXGIAdapter1 adapter).Success)
39-
{
40-
yield return new GraphicsCard(i, adapter.Description1.Description, adapter.Description1.VendorId, adapter.Description1.DeviceId);
41-
adapter.Dispose();
42-
i++;
43-
}
40+
yield return new GraphicsCard(i, adapter.Description1.Description, adapter.Description1.VendorId, adapter.Description1.DeviceId);
41+
adapter.Dispose();
42+
i++;
4443
}
44+
}
4545

46-
/// <inheritdoc />
47-
public IEnumerable<Display> GetDisplays(GraphicsCard graphicsCard)
48-
{
49-
using IDXGIAdapter1? adapter = _factory.GetAdapter1(graphicsCard.Index);
50-
51-
int i = 0;
52-
while (adapter.EnumOutputs(i, out IDXGIOutput output).Success)
53-
{
54-
int width = output.Description.DesktopCoordinates.Right - output.Description.DesktopCoordinates.Left;
55-
int height = output.Description.DesktopCoordinates.Bottom - output.Description.DesktopCoordinates.Top;
56-
yield return new Display(i, output.Description.DeviceName, width, height, graphicsCard);
57-
output.Dispose();
58-
i++;
59-
}
60-
}
46+
/// <inheritdoc />
47+
public IEnumerable<Display> GetDisplays(GraphicsCard graphicsCard)
48+
{
49+
using IDXGIAdapter1? adapter = _factory.GetAdapter1(graphicsCard.Index);
6150

62-
/// <inheritdoc />
63-
public IScreenCapture GetScreenCapture(Display display)
51+
int i = 0;
52+
while (adapter.EnumOutputs(i, out IDXGIOutput output).Success)
6453
{
65-
if (!_screenCaptures.TryGetValue(display, out DX11ScreenCapture? screenCapture))
66-
_screenCaptures.Add(display, screenCapture = new DX11ScreenCapture(_factory, display));
67-
return screenCapture;
54+
int width = output.Description.DesktopCoordinates.Right - output.Description.DesktopCoordinates.Left;
55+
int height = output.Description.DesktopCoordinates.Bottom - output.Description.DesktopCoordinates.Top;
56+
yield return new Display(i, output.Description.DeviceName, width, height, graphicsCard);
57+
output.Dispose();
58+
i++;
6859
}
60+
}
6961

70-
/// <inheritdoc />
71-
public void Dispose()
72-
{
73-
foreach (DX11ScreenCapture screenCapture in _screenCaptures.Values)
74-
screenCapture.Dispose();
75-
_screenCaptures.Clear();
62+
/// <inheritdoc />
63+
public IScreenCapture GetScreenCapture(Display display)
64+
{
65+
if (!_screenCaptures.TryGetValue(display, out DX11ScreenCapture? screenCapture))
66+
_screenCaptures.Add(display, screenCapture = new DX11ScreenCapture(_factory, display));
67+
return screenCapture;
68+
}
7669

77-
_factory.Dispose();
70+
/// <inheritdoc />
71+
public void Dispose()
72+
{
73+
foreach (DX11ScreenCapture screenCapture in _screenCaptures.Values)
74+
screenCapture.Dispose();
75+
_screenCaptures.Clear();
7876

79-
GC.SuppressFinalize(this);
80-
}
77+
_factory.Dispose();
8178

82-
#endregion
79+
GC.SuppressFinalize(this);
8380
}
84-
}
81+
82+
#endregion
83+
}

ScreenCapture.NET/Events/ScreenCaptureUpdatedEventArgs.cs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,33 @@
22

33
using System;
44

5-
namespace ScreenCapture.NET
5+
namespace ScreenCapture.NET;
6+
7+
/// <inheritdoc />
8+
/// <summary>
9+
/// Represents the information supplied with an <see cref="E:ScreenCapture.IDX11ScreenCapture.Updated" />-event.
10+
/// </summary>
11+
public class ScreenCaptureUpdatedEventArgs : EventArgs
612
{
7-
/// <inheritdoc />
13+
#region Properties & Fields
14+
815
/// <summary>
9-
/// Represents the information supplied with an <see cref="E:ScreenCapture.IDX11ScreenCapture.Updated" />-event.
16+
/// <c>true</c> if the update was successful; otherwise, <c>false</c>.
1017
/// </summary>
11-
public class ScreenCaptureUpdatedEventArgs : EventArgs
12-
{
13-
#region Properties & Fields
14-
15-
/// <summary>
16-
/// <c>true</c> if the update was successful; otherwise, <c>false</c>.
17-
/// </summary>
18-
public bool IsSuccessful { get; set; }
19-
20-
#endregion
18+
public bool IsSuccessful { get; set; }
2119

22-
#region Constructors
20+
#endregion
2321

24-
/// <summary>
25-
/// Initializes a new instance of the <see cref="ScreenCaptureUpdatedEventArgs"/> class.
26-
/// </summary>
27-
/// <param name="isSuccessful">Indicates if the last update was successful.</param>
28-
public ScreenCaptureUpdatedEventArgs(bool isSuccessful)
29-
{
30-
this.IsSuccessful = isSuccessful;
31-
}
22+
#region Constructors
3223

33-
#endregion
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="ScreenCaptureUpdatedEventArgs"/> class.
26+
/// </summary>
27+
/// <param name="isSuccessful">Indicates if the last update was successful.</param>
28+
public ScreenCaptureUpdatedEventArgs(bool isSuccessful)
29+
{
30+
this.IsSuccessful = isSuccessful;
3431
}
35-
}
32+
33+
#endregion
34+
}

ScreenCapture.NET/Helper/DPIAwareness.cs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,39 @@
33

44
using System.Runtime.InteropServices;
55

6-
namespace ScreenCapture.NET
6+
namespace ScreenCapture.NET;
7+
8+
/// <summary>
9+
/// Helper-class for DPI-related WIN-API calls.
10+
/// </summary>
11+
public static class DPIAwareness
712
{
8-
/// <summary>
9-
/// Helper-class for DPI-related WIN-API calls.
10-
/// </summary>
11-
public static class DPIAwareness
13+
[DllImport("user32.dll", SetLastError = true)]
14+
internal static extern bool SetProcessDpiAwarenessContext(int dpiFlag);
15+
16+
[DllImport("SHCore.dll", SetLastError = true)]
17+
internal static extern bool SetProcessDpiAwareness(PROCESS_DPI_AWARENESS awareness);
18+
19+
[DllImport("user32.dll")]
20+
internal static extern bool SetProcessDPIAware();
21+
22+
internal enum PROCESS_DPI_AWARENESS
1223
{
13-
[DllImport("user32.dll", SetLastError = true)]
14-
internal static extern bool SetProcessDpiAwarenessContext(int dpiFlag);
15-
16-
[DllImport("SHCore.dll", SetLastError = true)]
17-
internal static extern bool SetProcessDpiAwareness(PROCESS_DPI_AWARENESS awareness);
18-
19-
[DllImport("user32.dll")]
20-
internal static extern bool SetProcessDPIAware();
21-
22-
internal enum PROCESS_DPI_AWARENESS
23-
{
24-
Process_DPI_Unaware = 0,
25-
Process_System_DPI_Aware = 1,
26-
Process_Per_Monitor_DPI_Aware = 2
27-
}
28-
29-
internal enum DPI_AWARENESS_CONTEXT
30-
{
31-
DPI_AWARENESS_CONTEXT_UNAWARE = 16,
32-
DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = 17,
33-
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = 18,
34-
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = 34
35-
}
36-
37-
/// <summary>
38-
/// Sets the DPI-Awareness-Context to V2. This is needed to prevent issues when using desktop duplication.
39-
/// </summary>
40-
public static void Initalize() => SetProcessDpiAwarenessContext((int)DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
24+
Process_DPI_Unaware = 0,
25+
Process_System_DPI_Aware = 1,
26+
Process_Per_Monitor_DPI_Aware = 2
4127
}
42-
}
28+
29+
internal enum DPI_AWARENESS_CONTEXT
30+
{
31+
DPI_AWARENESS_CONTEXT_UNAWARE = 16,
32+
DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = 17,
33+
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = 18,
34+
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = 34
35+
}
36+
37+
/// <summary>
38+
/// Sets the DPI-Awareness-Context to V2. This is needed to prevent issues when using desktop duplication.
39+
/// </summary>
40+
public static void Initalize() => SetProcessDpiAwarenessContext((int)DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
41+
}
Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
11
using System;
22

3-
namespace ScreenCapture.NET
3+
namespace ScreenCapture.NET;
4+
5+
/// <summary>
6+
/// Represents the duplication of a single display.
7+
/// </summary>
8+
public interface IScreenCapture : IDisposable
49
{
510
/// <summary>
6-
/// Represents the duplication of a single display.
11+
/// Gets the <see cref="Display"/> this capture is duplicating.
12+
/// </summary>
13+
Display Display { get; }
14+
15+
/// <summary>
16+
/// Occurs when the <see cref="IScreenCapture"/> is updated.
17+
/// </summary>
18+
event EventHandler<ScreenCaptureUpdatedEventArgs>? Updated;
19+
20+
/// <summary>
21+
/// Attemts to capture the current frame showed on the <see cref="Display"/>.
22+
/// </summary>
23+
/// <returns><c>true</c> if the current frame was captures successfully; otherwise, <c>false</c>.</returns>
24+
bool CaptureScreen();
25+
26+
/// <summary>
27+
/// Creates a new <see cref="CaptureScreen"/> for this <see cref="IScreenCapture"/>.
28+
/// </summary>
29+
/// <param name="x">The x-location of the region to capture (must be &gt;= 0 and &lt; screen-width).</param>
30+
/// <param name="y">The y-location of the region to capture (must be &gt;= 0 and &lt; screen-height).</param>
31+
/// <param name="width">The width of the region to capture (must be &gt;= 0 and this + x must be &lt;= screen-width).</param>
32+
/// <param name="height">The height of the region to capture (must be &gt;= 0 and this + y must be &lt;= screen-height).</param>
33+
/// <param name="downscaleLevel">The level of downscaling applied to the image of this region before copying to local memory. The calculation is (width and height)/2^downscaleLevel.</param>
34+
/// <returns>The new <see cref="CaptureScreen"/>.</returns>
35+
CaptureZone RegisterCaptureZone(int x, int y, int width, int height, int downscaleLevel = 0);
36+
37+
/// <summary>
38+
/// Removes the given <see cref="CaptureScreen"/> from the <see cref="IScreenCapture"/>.
39+
/// </summary>
40+
/// <param name="captureZone">The previously registered <see cref="CaptureScreen"/>.</param>
41+
/// <returns><c>true</c> if the <see cref="CaptureScreen"/> was successfully removed; otherwise, <c>false</c>.</returns>
42+
bool UnregisterCaptureZone(CaptureZone captureZone);
43+
44+
/// <summary>
45+
/// Updates the the given <see cref="CaptureScreen"/>.
46+
/// </summary>
47+
/// <remarks>
48+
/// <c>null</c>-parameters are ignored and not changed.
49+
/// </remarks>
50+
/// <param name="captureZone">The previously registered <see cref="CaptureScreen"/>.</param>
51+
/// <param name="x">The new x-location of the region to capture (must be &gt;= 0 and &lt; screen-width).</param>
52+
/// <param name="y">The new y-location of the region to capture (must be &gt;= 0 and &lt; screen-height).</param>
53+
/// <param name="width">The width of the region to capture (must be &gt;= 0 and this + x must be &lt;= screen-width).</param>
54+
/// <param name="height">The new height of the region to capture (must be &gt;= 0 and this + y must be &lt;= screen-height).</param>
55+
/// <param name="downscaleLevel">The new level of downscaling applied to the image of this region before copying to local memory. The calculation is (width and height)/2^downscaleLevel.</param>
56+
void UpdateCaptureZone(CaptureZone captureZone, int? x = null, int? y = null, int? width = null, int? height = null, int? downscaleLevel = null);
57+
58+
/// <summary>
59+
/// Restarts the <see cref="IScreenCapture"/>.
760
/// </summary>
8-
public interface IScreenCapture : IDisposable
9-
{
10-
/// <summary>
11-
/// Gets the <see cref="Display"/> this capture is duplicating.
12-
/// </summary>
13-
Display Display { get; }
14-
15-
/// <summary>
16-
/// Occurs when the <see cref="IScreenCapture"/> is updated.
17-
/// </summary>
18-
event EventHandler<ScreenCaptureUpdatedEventArgs>? Updated;
19-
20-
/// <summary>
21-
/// Attemts to capture the current frame showed on the <see cref="Display"/>.
22-
/// </summary>
23-
/// <returns><c>true</c> if the current frame was captures successfully; otherwise, <c>false</c>.</returns>
24-
bool CaptureScreen();
25-
26-
/// <summary>
27-
/// Creates a new <see cref="CaptureScreen"/> for this <see cref="IScreenCapture"/>.
28-
/// </summary>
29-
/// <param name="x">The x-location of the region to capture (must be &gt;= 0 and &lt; screen-width).</param>
30-
/// <param name="y">The y-location of the region to capture (must be &gt;= 0 and &lt; screen-height).</param>
31-
/// <param name="width">The width of the region to capture (must be &gt;= 0 and this + x must be &lt;= screen-width).</param>
32-
/// <param name="height">The height of the region to capture (must be &gt;= 0 and this + y must be &lt;= screen-height).</param>
33-
/// <param name="downscaleLevel">The level of downscaling applied to the image of this region before copying to local memory. The calculation is (width and height)/2^downscaleLevel.</param>
34-
/// <returns>The new <see cref="CaptureScreen"/>.</returns>
35-
CaptureZone RegisterCaptureZone(int x, int y, int width, int height, int downscaleLevel = 0);
36-
37-
/// <summary>
38-
/// Removes the given <see cref="CaptureScreen"/> from the <see cref="IScreenCapture"/>.
39-
/// </summary>
40-
/// <param name="captureZone">The previously registered <see cref="CaptureScreen"/>.</param>
41-
/// <returns><c>true</c> if the <see cref="CaptureScreen"/> was successfully removed; otherwise, <c>false</c>.</returns>
42-
bool UnregisterCaptureZone(CaptureZone captureZone);
43-
44-
/// <summary>
45-
/// Updates the the given <see cref="CaptureScreen"/>.
46-
/// </summary>
47-
/// <remarks>
48-
/// <c>null</c>-parameters are ignored and not changed.
49-
/// </remarks>
50-
/// <param name="captureZone">The previously registered <see cref="CaptureScreen"/>.</param>
51-
/// <param name="x">The new x-location of the region to capture (must be &gt;= 0 and &lt; screen-width).</param>
52-
/// <param name="y">The new y-location of the region to capture (must be &gt;= 0 and &lt; screen-height).</param>
53-
/// <param name="width">The width of the region to capture (must be &gt;= 0 and this + x must be &lt;= screen-width).</param>
54-
/// <param name="height">The new height of the region to capture (must be &gt;= 0 and this + y must be &lt;= screen-height).</param>
55-
/// <param name="downscaleLevel">The new level of downscaling applied to the image of this region before copying to local memory. The calculation is (width and height)/2^downscaleLevel.</param>
56-
void UpdateCaptureZone(CaptureZone captureZone, int? x = null, int? y = null, int? width = null, int? height = null, int? downscaleLevel = null);
57-
58-
/// <summary>
59-
/// Restarts the <see cref="IScreenCapture"/>.
60-
/// </summary>
61-
void Restart();
62-
}
63-
}
61+
void Restart();
62+
}

0 commit comments

Comments
 (0)