Skip to content

Commit 98b0b60

Browse files
committed
*优化代码结构,消除冗余变量和类型转换,提升可读性
1 parent 50cd22a commit 98b0b60

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

Core.Window/ScreenCapture/ScreenCaptureByWGC.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Diagnostics;
2-
using System.Runtime.InteropServices;
3-
using System.Text;
1+
using System.Runtime.InteropServices;
42
using Windows.Graphics.Capture;
53
using OpenCvSharp;
64
using PluginCore;
@@ -55,11 +53,12 @@ IntPtr CreateForMonitor(
5553

5654
private ComPtr<ID3D11Resource> CreateSharpDxTexture2D(IDirect3DSurface surface) {
5755
unsafe {
58-
var iInspectable = (IInspectable)(object)surface;
56+
// ReSharper disable once SuspiciousTypeConversion.Global
57+
var iInspectable = (IInspectable)surface;
5958
var queryInterface = iInspectable.ObjRef.AsInterface<IDirect3DDxgiInterfaceAccess>();
60-
var d3dPointer = (void*)queryInterface.GetInterface(ID3D11Resource.Guid);
59+
var d3DPointer = (void*)queryInterface.GetInterface(ID3D11Resource.Guid);
6160
var comPtr = new ComPtr<ID3D11Resource>();
62-
comPtr.Handle = (ID3D11Resource*)d3dPointer;
61+
comPtr.Handle = (ID3D11Resource*)d3DPointer;
6362
return comPtr;
6463
}
6564
}
@@ -94,16 +93,14 @@ public static (ComPtr<IDXGIAdapter1>, OutputDesc1) GetAdapterForMonitor(ComPtr<I
9493
OutputDesc desc = new OutputDesc();
9594
output.GetDesc(ref desc);
9695
if (desc.Monitor == hMonitor) {
97-
ComPtr<IDXGIOutput6> output6 = null;
98-
if (output.QueryInterface(out output6) != 0)
96+
if (output.QueryInterface(out ComPtr<IDXGIOutput6> output6) != 0)
9997
throw new Exception("Failed to get IDXGIOutput6");
10098
var outputDesc = new OutputDesc1();
10199
if (output6.GetDesc1(ref outputDesc) != 0) throw new Exception("Failed to get Desc1");
102100
return (adapter, outputDesc);
103101
}
104-
else {
105-
output.Release();
106-
}
102+
103+
output.Release();
107104

108105
j++;
109106
}
@@ -168,10 +165,10 @@ public unsafe ScreenCaptureResult CaptureScreenMat(ScreenCaptureInfo screenCaptu
168165
ComPtr<ID3D11DeviceContext> immediateContext = default;
169166
ComPtr<ID3D11Device> d3DDevice = default;
170167
ComPtr<ID3D11Resource> stagingResource = default;
171-
Direct3D11CaptureFrame direct3D11CaptureFrame = null;
168+
Direct3D11CaptureFrame direct3D11CaptureFrame = null!;
172169
ComPtr<ID3D11Texture2D> stagingTexture = default;
173-
GraphicsCaptureSession session = null;
174-
Direct3D11CaptureFramePool framePool = null;
170+
GraphicsCaptureSession session = null!;
171+
Direct3D11CaptureFramePool framePool = null!;
175172
using var d3D11 = new D3D11(new DefaultNativeContext("d3d11"));
176173
try {
177174
using var factory = dxgi.CreateDXGIFactory1<IDXGIFactory1>();
@@ -192,11 +189,11 @@ public unsafe ScreenCaptureResult CaptureScreenMat(ScreenCaptureInfo screenCaptu
192189

193190
d3DDevice.GetImmediateContext(ref immediateContext);
194191

195-
IDirect3DDevice CreateDirect3DDeviceFromSharpDxDevice(ID3D11Device* d3dDevice) {
196-
IDirect3DDevice device = null;
192+
IDirect3DDevice CreateDirect3DDeviceFromSharpDxDevice(ID3D11Device* d3DDevice1) {
193+
IDirect3DDevice device = null!;
197194

198195
// Acquire the DXGI interface for the Direct3D device.
199-
using var dxgiDevice = d3dDevice->QueryInterface<IDXGIDevice3>();
196+
using var dxgiDevice = d3DDevice1->QueryInterface<IDXGIDevice3>();
200197
// Wrap the native device using a WinRT interop object.
201198
var hr = CreateDirect3D11DeviceFromDXGIDevice((IntPtr)dxgiDevice.Handle, out var pUnknown);
202199

@@ -247,7 +244,7 @@ IDirect3DDevice CreateDirect3DDeviceFromSharpDxDevice(ID3D11Device* d3dDevice) {
247244
if (d3DDevice.CreateTexture2D(&stagingTextureDesc, null, ref stagingTexture) != 0)
248245
throw new Exception("Failed to create staging texture");
249246

250-
stagingTexture.QueryInterface<ID3D11Resource>(out stagingResource);
247+
stagingTexture.QueryInterface(out stagingResource);
251248
immediateContext.CopyResource(stagingResource, bitmap);
252249
if (immediateContext.Map(stagingResource, 0, Map.Read, 0, &mappedSubresource) != 0)
253250
throw new Exception("Failed to map staging texture");

0 commit comments

Comments
 (0)