public Bitmap CaptureAndProcess(ModernCaptureItemDescription itemDescription) { // Assuming old texture is already disposed description = itemDescription; // Initialize DirectX context (for the canvas) textureSDRImage = new D3D11.Texture2D(d3dDevice, new D3D11.Texture2DDescription { Width = description.CanvasRect.Width, Height = description.CanvasRect.Height, MipLevels = 1, ArraySize = 1, Format = DXGI.Format.B8G8R8A8_UNorm_SRgb, Usage = D3D11.ResourceUsage.Default, SampleDescription = new DXGI.SampleDescription(1, 0), BindFlags = D3D11.BindFlags.RenderTarget, CpuAccessFlags = D3D11.CpuAccessFlags.None, OptionFlags = D3D11.ResourceOptionFlags.None, }); rtvSdrTexture = new D3D11.RenderTargetView(d3dDevice, textureSDRImage); d3dContext.Rasterizer.SetViewport(new Viewport(0, 0, description.CanvasRect.Width, description.CanvasRect.Height)); d3dContext.OutputMerger.SetRenderTargets(rtvSdrTexture); d3dContext.ClearRenderTargetView(rtvSdrTexture, new SharpDX.Mathematics.Interop.RawColor4 { A = 0.0f, B = 0.0f, G = 0.0f, R = 0.0f }); foreach (var item in description.Regions) { using (var session = new ModernCaptureMonitorSession(wrtD3D11Device, item)) { Direct3D11CaptureFrame f; currentSession = session; session.Session.StartCapture(); while ((f = session.FramePool.TryGetNextFrame()) == null) { Thread.Sleep(1); } ProcessFrame(f); f.Dispose(); } } // Process final 8-bit bitmap var gdiPlusBitmap = new Bitmap(DumpAndSaveImage()); rtvSdrTexture.Dispose(); textureSDRImage.Dispose(); description = null; #if DEBUG System.Diagnostics.Debug.WriteLine(SharpDX.Diagnostics.ObjectTracker.ReportActiveObjects()); #endif return(gdiPlusBitmap); }
private Bitmap CaptureRectangleDirect3D11(IntPtr handle, Rectangle rect, bool captureCursor = false) { var captureMonRegions = new List <ModernCaptureMonitorDescription>(); Bitmap bmp; if (rect.Width == 0 || rect.Height == 0) { return(null); } // 1. Get regions and the HDR metadata information foreach (var monitor in MonitorEnumerationHelper.GetMonitors()) { if (monitor.MonitorArea.IntersectsWith(rect)) { var screenBoundCopy = monitor.MonitorArea.Copy(); screenBoundCopy.Intersect(rect); captureMonRegions.Add(new ModernCaptureMonitorDescription { DestGdiRect = screenBoundCopy, HdrMetadata = HdrMetadataUtility.GetHdrMetadataForMonitor(monitor.DeviceName), MonitorInfo = monitor, CaptureCursor = captureCursor, }); } } // 2. Compose a list of rects for capture var catpureItem = new ModernCaptureItemDescription(rect, captureMonRegions); // 3. Request capture and wait for bitmap // 3.1 Determine rects and transform them to DirectX coordinate system // 3.2 Capture and wait for content // 3.3 Shader and draw passes // 3.4 Datastream pass, copy var d3dCapture = ModernCaptureSignletonManager.Instance.Take(); bmp = d3dCapture.CaptureAndProcess(catpureItem); ModernCaptureSignletonManager.Instance.Release(); return(bmp); }