private IntPtr GetSharedHandle(SharpDX.Direct3D10.Texture2D Texture) { SharpDX.DXGI.Resource resource = Texture.QueryInterface <SharpDX.DXGI.Resource>(); IntPtr result = resource.SharedHandle; resource.Dispose(); return(result); }
private void dislpayTimer_Tick(object sender, EventArgs ev) { // duplicate output stuff SharpDX.DXGI.Resource screenResource = null; SharpDX.DataStream dataStream; SharpDX.DXGI.Surface screenSurface; try { SharpDX.DXGI.OutputDuplicateFrameInformation duplicateFrameInformation; duplicatedOutput.AcquireNextFrame(1000, out duplicateFrameInformation, out screenResource); } catch (SharpDX.SharpDXException e) { if (e.ResultCode.Code == SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code) { // keep retrying return; } else { throw e; } } // copy resource into memory that can be accessed by the CPU c_device.ImmediateContext.CopyResource(screenResource.QueryInterface <SharpDX.Direct3D11.Resource>(), screenTexture); // cast from texture to surface, so we can access its bytes screenSurface = screenTexture.QueryInterface <SharpDX.DXGI.Surface>(); // map the resource to access it screenSurface.Map(SharpDX.DXGI.MapFlags.Read, out dataStream); getImageFromDXStream(dataStream); captureBox.Image = target; // free resources dataStream.Close(); screenSurface.Unmap(); screenSurface.Dispose(); screenResource.Dispose(); duplicatedOutput.ReleaseFrame(); // print how many frames we could process within the last second Console.WriteLine("fps: {0}", 1f / sw.Elapsed.TotalSeconds); sw.Reset(); sw.Start(); }
private void OnRender(IntPtr handle, bool isNewSurface) { if (isNewSurface) { if (brush != null) { brush.Dispose(); brush = null; } if (renderTarget != null) { renderTarget.Dispose(); renderTarget = null; } SharpDX.ComObject comObject = new SharpDX.ComObject(handle); SharpDX.DXGI.Resource resource = comObject.QueryInterface <SharpDX.DXGI.Resource>(); SharpDX.Direct3D10.Texture2D texture = resource.QueryInterface <SharpDX.Direct3D10.Texture2D>(); using (var surface = texture.QueryInterface <SharpDX.DXGI.Surface>()) { var properties = new RenderTargetProperties(); properties.DpiX = 96; properties.DpiY = 96; properties.MinLevel = FeatureLevel.Level_DEFAULT; properties.PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, AlphaMode.Premultiplied); properties.Type = RenderTargetType.Default; properties.Usage = RenderTargetUsage.None; renderTarget = new RenderTarget(new Factory(), surface, properties); } } if (brush == null) { brush = new SharpDX.Direct2D1.SolidColorBrush(renderTarget, new SharpDX.Color4(0.2f, 0.2f, 0.2f, 0.5f)); } renderTarget.BeginDraw(); renderTarget.DrawTextLayout(new SharpDX.Vector2(50, 50), textLayout, brush); renderTarget.EndDraw(); }
private void D3DImage_OnRender(IntPtr surface, bool isNewSurface) { if (isNewSurface) { Context.DestroyBuffers(); // Yikes - from https://github.com/microsoft/WPFDXInterop/blob/master/samples/D3D11Image/D3D11Visualization/D3DVisualization.cpp#L384 ComObject res = ComObject.FromPointer <ComObject>(surface); SharpDX.DXGI.Resource resource = res.QueryInterface <SharpDX.DXGI.Resource>(); IntPtr sharedHandle = resource.SharedHandle; resource.Dispose(); SharpDX.Direct3D11.Resource d3dres = Context.Device.OpenSharedResource <SharpDX.Direct3D11.Resource>(sharedHandle); Context.UpdateSize(RenderWidth, RenderHeight, (int width, int height) => d3dres.QueryInterface <Texture2D>()); d3dres.Dispose(); } LastUpdatedTime = Context.UpdateScene((DateTime.Now - LastUpdatedTime).Milliseconds / 100.0f); // TODO: Measure elapsed time! Context.RenderScene(); Render?.Invoke(this, new EventArgs()); Context.ImmediateContext.Flush(); }
/// <summary> /// <p>Indicates that the application is ready to process the next desktop image.</p> /// </summary> /// <param name="timeoutInMilliseconds"><dd> <p>The time-out interval, in milliseconds. This interval specifies the amount of time that this method waits for a new frame before it returns to the caller. This method returns if the interval elapses, and a new desktop image is not available.</p> <p>For more information about the time-out interval, see Remarks.</p> </dd></param> /// <param name="frameInfoRef"><dd> <p>A reference to a memory location that receives the <strong><see cref="SharpDX.DXGI.OutputDuplicateFrameInformation"/></strong> structure that describes timing and presentation statistics for a frame.</p> </dd></param> /// <param name="desktopResourceOut"><dd> <p>A reference to a variable that receives the <strong><see cref="SharpDX.DXGI.Resource"/></strong> interface of the surface that contains the desktop bitmap.</p> </dd></param> /// <remarks> /// <p>When <strong>AcquireNextFrame</strong> returns successfully, the calling application can access the desktop image that <strong>AcquireNextFrame</strong> returns in the variable at <em>ppDesktopResource</em>. /// If the caller specifies a zero time-out interval in the <em>TimeoutInMilliseconds</em> parameter, <strong>AcquireNextFrame</strong> verifies whether there is a new desktop image available, returns immediately, and indicates its outcome with the return value. If the caller specifies an <strong>INFINITE</strong> time-out interval in the <em>TimeoutInMilliseconds</em> parameter, the time-out interval never elapses.</p><strong>Note</strong>??You cannot cancel the wait that you specified in the <em>TimeoutInMilliseconds</em> parameter. Therefore, if you must periodically check for other conditions (for example, a terminate signal), you should specify a non-<strong>INFINITE</strong> time-out interval. After the time-out interval elapses, you can check for these other conditions and then call <strong>AcquireNextFrame</strong> again to wait for the next frame.?<p><strong>AcquireNextFrame</strong> acquires a new desktop frame when the operating system either updates the desktop bitmap image or changes the shape or position of a hardware reference. The new frame that <strong>AcquireNextFrame</strong> acquires might have only the desktop image updated, only the reference shape or position updated, or both.</p> /// </remarks> /// <include file='.\..\..\Documentation\CodeComments.xml' path="/comments/comment[@id='IDXGIOutputDuplication::AcquireNextFrame']/*"/> /// <msdn-id>hh404615</msdn-id> /// <unmanaged>HRESULT IDXGIOutputDuplication::AcquireNextFrame([In] unsigned int TimeoutInMilliseconds,[Out] DXGI_OUTDUPL_FRAME_INFO* pFrameInfo,[Out] IDXGIResource** ppDesktopResource)</unmanaged> /// <unmanaged-short>IDXGIOutputDuplication::AcquireNextFrame</unmanaged-short> public void AcquireNextFrame(int timeoutInMilliseconds, out SharpDX.DXGI.OutputDuplicateFrameInformation frameInfoRef, out SharpDX.DXGI.Resource desktopResourceOut) { var result = this.TryAcquireNextFrame(timeoutInMilliseconds, out frameInfoRef, out desktopResourceOut); result.CheckError(); }