private RenderTarget2D CreateRenderTarget()
        {
            var actualWidth  = (int)ActualWidth;
            var actualHeight = (int)ActualHeight;

            if (actualWidth == 0 || actualHeight == 0)
            {
                return(null);
            }
            if (GraphicsDevice == null)
            {
                return(null);
            }
            var renderTarget = new RenderTarget2D(GraphicsDevice, actualWidth, actualHeight, false,
                                                  SurfaceFormat.Bgra32, DepthFormat.Depth24Stencil8, 1, RenderTargetUsage.PlatformContents, true);
            var handle = renderTarget.GetSharedHandle();

            if (handle == IntPtr.Zero)
            {
                throw new ArgumentException("Handle could not be retrieved");
            }
            _renderTargetD3D9 = new SharpDX.Direct3D9.Texture(_graphicsDeviceService.Direct3DDevice, renderTarget.Width,
                                                              renderTarget.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, SharpDX.Direct3D9.Format.A8R8G8B8,
                                                              SharpDX.Direct3D9.Pool.Default, ref handle);
            using (var surface = _renderTargetD3D9.GetSurfaceLevel(0)) {
                _direct3DImage.Lock();
                _direct3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer);
                _direct3DImage.Unlock();
            }
            return(renderTarget);
        }
        public void GetSharedHandle()
        {
            var rt           = new RenderTarget2D(gd, 16, 16, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PlatformContents, true);
            var sharedHandle = rt.GetSharedHandle();

            Assert.AreNotEqual(sharedHandle, IntPtr.Zero);

            var resource = SharpDX.CppObject.FromPointer <SharpDX.DXGI.Resource>(sharedHandle);

            rt.Dispose();
        }