public void CreateD3D9TextureFromD3D10Texture(SharpDX.Direct3D10.Texture2D Texture)
        {
            DirectXHelpers.SafeDispose(ref shareableTexture9);

            if (IsShareable(Texture))
            {
                SharpDX.Direct3D9.Format format = TranslateFormat(Texture);
                if (format == SharpDX.Direct3D9.Format.Unknown)
                {
                    throw new ArgumentException("Texture format is not compatible with OpenSharedResource");
                }

                IntPtr Handle = GetSharedHandle(Texture);
                if (Handle == IntPtr.Zero)
                {
                    throw new ArgumentNullException("Handle");
                }

                shareableTexture9 = new SharpDX.Direct3D9.Texture(sharpDXGraphicsDeviceService9.GraphicsDevice, Texture.Description.Width, Texture.Description.Height, 1, SharpDX.Direct3D9.Usage.RenderTarget, format, SharpDX.Direct3D9.Pool.Default, ref Handle);
            }
            else
            {
                throw new ArgumentException("Texture must be created with ResourceOptionFlags.Shared");
            }
        }
 /// <summary>
 /// Releases a reference to the singleton instance.
 /// </summary>
 public void Release(bool disposing)
 {
     // Decrement the "how many controls sharing the device" reference count.
     if (Interlocked.Decrement(ref referenceCount) == 0)
     {
         // If this is the last control to finish using the
         // device, we should dispose the singleton instance.
         DirectXHelpers.SafeDispose(ref renderTarget);
         DirectXHelpers.SafeDispose(ref texture);
         DirectXHelpers.SafeDispose(ref shareableTexture);
         DirectXHelpers.SafeDispose(ref factoryDXGI);
         DirectXHelpers.SafeDispose(ref factory2D);
         DirectXHelpers.SafeDispose(ref shareableTexture9);
     }
 }
        public void ResizeDevice(int width, int height)
        {
            lock (this)
            {
                if (width < 0)
                {
                    throw new ArgumentOutOfRangeException("width", "Value must be positive.");
                }
                if (height < 0)
                {
                    throw new ArgumentOutOfRangeException("height", "Value must be positive.");
                }
                if ((width <= this.width) && (height <= this.height))
                {
                    return;
                }

                DirectXHelpers.SafeDispose(ref this.texture);
                var texture = CreateTexture(Math.Max(width, this.width), Math.Max(height, this.height), true);
                this.texture = texture;

                DirectXHelpers.SafeDispose(ref this.shareableTexture);
                var shareableTexture = CreateTexture(Math.Max(width, this.width), Math.Max(height, this.height), false);
                this.shareableTexture = shareableTexture;

                CreateD3D9TextureFromD3D10Texture(shareableTexture);

                this.width  = texture.Description.Width;
                this.height = texture.Description.Height;

                using (SharpDX.DXGI.Surface surface = texture.AsSurface())
                {
                    CreateRenderTarget(surface);
                }

                if (DeviceResized != null)
                {
                    DeviceResized(this, EventArgs.Empty);
                }
            }
        }