void DiscardDeviceResources()
 {
     if (_d2dContext != null)
     {
         DXUtil.Dispose(ref _blur);
         DXUtil.Dispose(ref _warp);
         DXUtil.Dispose(ref _imBmp);
         DXUtil.Dispose(ref _targetBmp);
         _d2dDevice.Dispose();
         _lineBrush.Dispose();
         _d2dContext.Dispose();
         _d2dContext = null;
     }
 }
        //----------------------------------------------------------------------
        #region ** device resources

        void CreateDeviceResources()
        {
            D3D.FeatureLevel  actualLevel;
            D3D.DeviceContext d3dContext = null;
            var d3dDevice = new D3D.Device(IntPtr.Zero);
            var result    = HResult.Ok;

            for (int i = 0; i <= 1; i++)
            {
                // use WARP if hardware is not available
                var dt = i == 0 ? D3D.DriverType.Hardware : D3D.DriverType.Warp;
                result = D3D.D3D11.CreateDevice(null, dt, IntPtr.Zero, D3D.DeviceCreationFlags.BgraSupport | D3D.DeviceCreationFlags.SingleThreaded,
                                                null, 0, D3D.D3D11.SdkVersion, d3dDevice, out actualLevel, out d3dContext);
                if (result.Code != unchecked ((int)0x887A0004)) // DXGI_ERROR_UNSUPPORTED
                {
                    break;
                }
            }
            result.CheckError();
            d3dContext.Dispose();

            var dxgiDevice = d3dDevice.QueryInterface <DXGI.Device>();

            d3dDevice.Dispose();

            _d2dDevice = D2D.Device.Create(_d2dFactory, dxgiDevice);
            dxgiDevice.Dispose();

            _d2dContext = D2D.DeviceContext.Create(_d2dDevice, D2D.DeviceContextOptions.None);
            _d2dContext.SetUnitMode(D2D.UnitMode.Pixels);

            _lineBrush = D2D.SolidColorBrush.Create(_d2dContext, ColorF.Blue);

            var sourceEffect = D2D.Effect.RegisterAndCreateCustom <WarpEffect>(_d2dFactory, _d2dContext);

            _warp        = (WarpEffect)sourceEffect.CustomEffect;
            _warp.Effect = sourceEffect;

            _blur = D2D.Effects.GaussianBlur.Create(_d2dContext);
            _blur.StandardDeviation = 2f;
        }