public void Close()
            {
                deviceReady = false;

                if (screenTexture != null && !screenTexture.IsDisposed)
                {
                    screenTexture.Dispose();
                    screenTexture = null;
                }

                if (screenTarget != null && !screenTarget.IsDisposed)
                {
                    screenTarget.Dispose();
                    screenTarget = null;
                }

                if (duplTexture != null && !duplTexture.IsDisposed)
                {
                    duplTexture.Dispose();
                    duplTexture = null;
                }

                if (deskDupl != null && !deskDupl.IsDisposed)
                {
                    deskDupl.Dispose();
                    deskDupl = null;
                }

                if (cursorInfo != null)
                {
                    cursorInfo.Dispose();
                    cursorInfo = null;
                }
            }
Exemplo n.º 2
0
        public void Init()
        {
            lock (_syncLock)
            {
                try
                {
                    _acquireTask?.Wait();
                }
                catch { }

                _acquireTask = null;
                _deskDupl?.Dispose();

                try
                {
                    _deskDupl = _output.DuplicateOutput(_device);
                }
                catch (SharpDXException e) when(e.Descriptor == ResultCode.NotCurrentlyAvailable)
                {
                    throw new Exception(
                              "There is already the maximum number of applications using the Desktop Duplication API running, please close one of the applications and try again.",
                              e);
                }
                catch (SharpDXException e) when(e.Descriptor == ResultCode.Unsupported)
                {
                    throw new NotSupportedException(
                              "Desktop Duplication is not supported on this system.\nIf you have multiple graphic cards, try running Captura on integrated graphics.",
                              e);
                }
            }
        }
Exemplo n.º 3
0
        public DesktopDuplicator(Rectangle Rect, bool IncludeCursor, int Monitor, int Adapter = 0)
        {
            _rect          = Rect;
            _includeCursor = IncludeCursor;

            Adapter1 adapter;

            try
            {
                adapter = new Factory1().GetAdapter1(Adapter);
            }
            catch (SharpDXException e)
            {
                throw new Exception("Could not find the specified graphics card adapter.", e);
            }

            _device = new Device(adapter);

            Output output;

            try
            {
                output = adapter.GetOutput(Monitor);
            }
            catch (SharpDXException e)
            {
                throw new Exception("Could not find the specified output device.", e);
            }

            var output1 = output.QueryInterface <Output1>();

            _outputDesc = output.Description;

            _textureDesc = new Texture2DDescription
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = _rect.Width,
                Height            = _rect.Height,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = ResourceUsage.Staging
            };

            try
            {
                _deskDupl = output1.DuplicateOutput(_device);
            }
            catch (SharpDXException e) when(e.Descriptor == SharpDX.DXGI.ResultCode.NotCurrentlyAvailable)
            {
                throw new Exception("There is already the maximum number of applications using the Desktop Duplication API running, please close one of the applications and try again.", e);
            }
            catch (SharpDXException e) when(e.Descriptor == SharpDX.DXGI.ResultCode.Unsupported)
            {
                throw new NotSupportedException("Desktop Duplication is not supported on this system.\nIf you have multiple graphic cards, try running Captura on integrated graphics.", e);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///   Acquires a frame from the desktop duplication instance with the specified index
        /// </summary>
        /// <param name="index">Index of the desktop duplication instance</param>
        private void AcquireFrame(int index)
        {
            OutputDuplicateFrameInformation info;
            Resource          desktopResource = null;
            OutputDuplication duplication     = this.duplications[index];

            do
            {
                // release previous frame if last capture attempt failed
                if (desktopResource != null)
                {
                    desktopResource.Dispose();
                    duplication.ReleaseFrame();
                }

                // try to capture a frame
                duplication.AcquireNextFrame(DuplicationFrameTimeout,
                                             out info,
                                             out desktopResource);
            } while (info.TotalMetadataBufferSize == 0);

            LastPresentTime = info.LastPresentTime;
            this.devices[index]
            .ImmediateContext.CopySubresourceRegion(desktopResource.QueryInterface <SharpDX.Direct3D11.Resource>(),
                                                    0,
                                                    this.regions[index],
                                                    StagingTextures[index],
                                                    0);

            // release resources
            desktopResource.Dispose();
            duplication.ReleaseFrame();
        }
        /// <summary>
        /// Duplicates the output of the specified monitor on the specified graphics adapter.
        /// </summary>
        /// <param name="whichGraphicsCardAdapter">The adapter which contains the desired outputs.</param>
        /// <param name="whichOutputDevice">The output device to duplicate (i.e. monitor). Begins with zero, which seems to correspond to the primary monitor.</param>
        public DesktopDuplicator(int whichGraphicsCardAdapter, int whichOutputDevice)
        {
            _whichOutputDevice = whichOutputDevice;
            using (var adapter = CreateAdapter(whichGraphicsCardAdapter))
            {
                _device = new Device(adapter);
                using (var output1 = CreateOutput1(whichOutputDevice, adapter))
                {
                    _outputDescription  = output1.Description;
                    _textureDescription = new Texture2DDescription()
                    {
                        CpuAccessFlags    = CpuAccessFlags.Read,
                        BindFlags         = BindFlags.None,
                        Format            = Format.B8G8R8A8_UNorm,
                        Width             = _outputDescription.DesktopBounds.Width,
                        Height            = _outputDescription.DesktopBounds.Height,
                        OptionFlags       = ResourceOptionFlags.None,
                        MipLevels         = 1,
                        ArraySize         = 1,
                        SampleDescription = { Count = 1, Quality = 0 },
                        Usage             = ResourceUsage.Staging
                    };

                    _deskDupl = CreateOutputDuplication(output1);
                }
            }
        }
        public Result ProcessFrame(Action <DataBox, Texture2DDescription> processAction, int timeoutInMilliseconds = 5)
        {
            //截屏,可能失败
            using OutputDuplication duplicatedOutput = output1.DuplicateOutput(device);
            var result = duplicatedOutput.TryAcquireNextFrame(timeoutInMilliseconds, out OutputDuplicateFrameInformation duplicateFrameInformation, out SharpDX.DXGI.Resource screenResource);

            if (!result.Success)
            {
                return(result);
            }

            using Texture2D screenTexture2D = screenResource.QueryInterface <Texture2D>();

            //复制数据
            device.ImmediateContext.CopyResource(screenTexture2D, screenTexture);
            DataBox mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None);

            processAction?.Invoke(mapSource, textureDesc);

            //释放资源
            device.ImmediateContext.UnmapSubresource(screenTexture, 0);
            screenResource.Dispose();
            duplicatedOutput.ReleaseFrame();

            return(result);
        }
Exemplo n.º 7
0
    public ScreenCapture(int numAdapter, int numOutput)
    {
        var      factory = new Factory1();
        Adapter1 adapter = new Factory1().GetAdapter1(numAdapter);

        device = new Device(adapter);
        Output output  = adapter.GetOutput(numOutput);
        var    output1 = output.QueryInterface <Output1>();

        outputDescription = output.Description;
        outputDuplication = output1.DuplicateOutput(device);

        width  = outputDescription.DesktopBounds.GetWidth();
        height = outputDescription.DesktopBounds.GetHeight();

        desktopImageTexture = new Texture2D(device, new Texture2DDescription()
        {
            CpuAccessFlags    = CpuAccessFlags.Read,
            BindFlags         = BindFlags.None,
            Format            = Format.B8G8R8A8_UNorm,
            Width             = width,
            Height            = height,
            OptionFlags       = ResourceOptionFlags.None,
            MipLevels         = 1,
            ArraySize         = 1,
            SampleDescription = { Count = 1, Quality = 0 },
            Usage             = ResourceUsage.Staging
        });

        image      = new Bitmap(width, height, PixelFormat.Format32bppRgb);
        boundsRect = new Rectangle(0, 0, width, height);
    }
        private void InitDesktopDuplicator()
        {
            // Duplicate the output
            _duplicatedOutput = _output1.DuplicateOutput(_device);

            _desktopDuplicatorInvalid = false;
        }
        internal void Dispose()
        {
            if (_outputDuplication != null)
            {
                _outputDuplication.Dispose();
                _outputDuplication = null;
            }

            if (_device != null)
            {
                _device.Dispose();
                _device = null;
            }

            if (_deviceContext != null)
            {
                _deviceContext.Dispose();
                _deviceContext = null;
            }

            if (_adapter != null)
            {
                _adapter.Dispose();
                _adapter = null;
            }
        }
Exemplo n.º 10
0
        private void Initialize(int whichGraphicsCardAdapter, int whichOutputDevice)
        {
            var adapter = new Factory1().GetAdapter1(whichGraphicsCardAdapter);

            this._device = new Device(adapter);
            Output output = adapter.GetOutput(whichOutputDevice);

            var output1 = output.QueryInterface <Output1>();

            this._outputDescription         = output.Description;
            this._desktopTextureDescription = new Texture2DDescription()
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = this._outputDescription.DesktopBounds.Width(),
                Height            = this._outputDescription.DesktopBounds.Height(),
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = ResourceUsage.Staging
            };

            this._desktopImageTexture = new Texture2D(_device, _desktopTextureDescription);
            this._desktopDuplication  = output1.DuplicateOutput(_device);
        }
Exemplo n.º 11
0
        private void Initialize()
        {
            factory = new Factory1();
            //Get first adapter
            adapter = factory.GetAdapter1(0);
            //Get device from adapter
            device = new SharpDX.Direct3D11.Device(adapter);
            //Get front buffer of the adapter
            output  = adapter.GetOutput(0);
            output1 = output.QueryInterface <Output1>();

            // Width/Height of desktop to capture
            int width  = output.Description.DesktopBounds.Right;
            int height = output.Description.DesktopBounds.Bottom;

            size = new Vector2Int(width, height);

            // Create Staging texture CPU-accessible
            textureDesc = new Texture2DDescription {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = ResourceUsage.Staging
            };
            screenTexture    = new Texture2D(device, textureDesc);
            duplicatedOutput = output1.DuplicateOutput(device);
        }
Exemplo n.º 12
0
        private void Initialize()
        {
            _factory = new Factory1();

            var adapterOutput = _factory.Adapters1
                                .Select(adapter => new { adapter, adapter.Outputs })
                                .SelectMany(_ => _.Outputs.Select(output => new { _.adapter, output }))
                                .FirstOrDefault(_ => _.output.Description.MonitorHandle == Screen.HMon) ?? throw new InvalidOperationException();

            _device  = new Device(adapterOutput.adapter);
            _output  = adapterOutput.output;
            _adapter = adapterOutput.adapter;
            _output1 = _output.QueryInterface <Output1>();

            _screenTexture = new Texture2D(_device, new Texture2DDescription
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = _width,
                Height            = _height,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = ResourceUsage.Staging
            });

            _duplicatedOutput = _output1.DuplicateOutput(_device);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Init some variables one times to spend execution time.
        /// </summary>
        public DirectX()
        {
            try
            {
                factory = new Factory1();
                adapter = factory.GetAdapter1(numAdapter);
                device  = new Device(adapter);
                output  = adapter.GetOutput(numOutput);
                output1 = output.QueryInterface <Output1>();
                // get screen wize

                textureDesc = new Texture2DDescription
                {
                    CpuAccessFlags = CpuAccessFlags.Read,
                    BindFlags      = BindFlags.None,
                    Format         = Format.B8G8R8A8_UNorm,
                    Width          = ((SharpDX.Mathematics.Interop.RawRectangle)output.Description.DesktopBounds).Right - ((SharpDX.Mathematics.Interop.RawRectangle)output.Description.DesktopBounds).Left,
                    Height         = ((SharpDX.Mathematics.Interop.RawRectangle)output.Description.DesktopBounds).Bottom - ((SharpDX.Mathematics.Interop.RawRectangle)output.Description.DesktopBounds).Top,

                    OptionFlags       = ResourceOptionFlags.None,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Staging
                };

                screenTexture    = new Texture2D(device, textureDesc);
                duplicatedOutput = output1.DuplicateOutput(device);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 14
0
 public void Dispose()
 {
     Adapter?.Dispose();
     Device?.Dispose();
     OutputDuplication?.Dispose();
     Texture2D?.Dispose();
 }
Exemplo n.º 15
0
        /// <summary>
        /// Duplicates the output of the specified monitor on the specified graphics adapter.
        /// </summary>
        /// <param name="whichGraphicsCardAdapter">The adapter which contains the desired outputs.</param>
        /// <param name="whichOutputDevice">The output device to duplicate (i.e. monitor). Begins with zero, which seems to correspond to the primary monitor.</param>
        public DesktopDuplicator(int whichGraphicsCardAdapter, int whichOutputDevice)
        {
            try
            {
                adapter = new Factory1().GetAdapter1(whichGraphicsCardAdapter);
            }
            catch (SharpDXException)
            {
                throw new DesktopDuplicationException("Could not find the specified graphics card adapter.");
            }
            try
            {
                mDevice = new Device(adapter);
            }
            catch (SharpDXException)
            {
                throw new DesktopDuplicationException("Could not create new Device");
            }
            mWhichOutputDevice = whichOutputDevice;

            Output output = null;

            try
            {
                output = adapter.GetOutput(whichOutputDevice);
            }
            catch (SharpDXException)
            {
                throw new DesktopDuplicationException("Could not find the specified output device.");
            }

            var output1 = output.QueryInterface <Output1>();

            mOutputDesc  = output.Description;
            mTextureDesc = new Texture2DDescription
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = mOutputDesc.DesktopBounds.Width,
                Height            = mOutputDesc.DesktopBounds.Height,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = ResourceUsage.Staging
            };

            try
            {
                mDeskDupl = output1.DuplicateOutput(mDevice);
            }
            catch (SharpDXException ex)
            {
                if (ex.ResultCode.Code == ResultCode.NotCurrentlyAvailable.Result.Code)
                {
                    throw new DesktopDuplicationException("There is already the maximum number of applications using the Desktop Duplication API running, please close one of the applications and try again.");
                }
            }
        }
Exemplo n.º 16
0
        public SharpDX.DataStream CaptureScreen()
        {
            // try to get duplicated frame within given time
            try
            {
                OutputDuplicateFrameInformation duplicateFrameInformation;
                duplicatedOutput.AcquireNextFrame(1000, out duplicateFrameInformation, out screenResource);

                // copy resource into memory that can be accessed by the CPU
                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(MapFlags.Read, out dataStream);

                // seek within the stream and read one byte
                //dataStream.Position = 4;
                //dataStream.ReadByte();

                // free resources
                //dataStream.Close();
                screenSurface.Unmap();
                screenSurface.Dispose();
                screenResource.Dispose();
                duplicatedOutput.ReleaseFrame();
                return(dataStream);
            }
            catch (SharpDX.SharpDXException e)
            {
                device  = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware);
                factory = new Factory1();
                // creating CPU-accessible texture resource
                texdes = new SharpDX.Direct3D11.Texture2DDescription();
                texdes.CpuAccessFlags            = SharpDX.Direct3D11.CpuAccessFlags.Read;
                texdes.BindFlags                 = SharpDX.Direct3D11.BindFlags.None;
                texdes.Format                    = Format.B8G8R8A8_UNorm;
                texdes.Height                    = factory.Adapters1[numAdapter].Outputs[numOutput].Description.DesktopBounds.Height;
                texdes.Width                     = factory.Adapters1[numAdapter].Outputs[numOutput].Description.DesktopBounds.Width;
                texdes.OptionFlags               = SharpDX.Direct3D11.ResourceOptionFlags.None;
                texdes.MipLevels                 = 1;
                texdes.ArraySize                 = 1;
                texdes.SampleDescription.Count   = 1;
                texdes.SampleDescription.Quality = 0;
                texdes.Usage                     = SharpDX.Direct3D11.ResourceUsage.Staging;
                screenTexture                    = new SharpDX.Direct3D11.Texture2D(device, texdes);

                // duplicate output stuff
                output = new Output1(factory.Adapters1[numAdapter].Outputs[numOutput].NativePointer);
                try
                {
                    duplicatedOutput = output.DuplicateOutput(device);
                }
                catch { }
                return(CaptureScreen());
            }
        }
Exemplo n.º 17
0
        public void StartCapture(IntPtr hWnd, Device device, Factory factory)
        {
            using var factory1 = factory.QueryInterface <Factory1>();
            using var adapter  = factory1.GetAdapter1(0);
            using var output1  = adapter.GetOutput(0).QueryInterface <Output1>();

            _duplication = output1.DuplicateOutput(device);
            IsCapturing  = true;
        }
Exemplo n.º 18
0
 public DirectXOutput(Adapter1 adapter,
                      SharpDX.Direct3D11.Device device,
                      OutputDuplication outputDuplication,
                      Texture2D texture2D)
 {
     Adapter           = adapter;
     Device            = device;
     OutputDuplication = outputDuplication;
     Texture2D         = texture2D;
 }
Exemplo n.º 19
0
        /// <summary>
        /// Duplicates the output of the specified monitor on the specified graphics adapter.
        /// </summary>
        /// <param name="whichGraphicsCardAdapter">The adapter which contains the desired outputs.</param>
        /// <param name="whichOutputDevice">The output device to duplicate (i.e. monitor). Begins with zero, which seems to correspond to the primary monitor.</param>
        public DesktopDuplicator(int whichGraphicsCardAdapter, int whichOutputDevice, VSyncLevel vSync)
        {
            this.vSync = vSync;
            this.mWhichOutputDevice = whichOutputDevice;
            Adapter1 adapter;

            try
            {
                adapter = new Factory1().GetAdapter1(whichGraphicsCardAdapter);
            }
            catch (SharpDXException)
            {
                throw new DesktopDuplicationException("Could not find the specified graphics card adapter.");
            }
            this.mDevice = new Device(adapter, DeviceCreationFlags.SingleThreaded | DeviceCreationFlags.PreventAlteringLayerSettingsFromRegistry);

            Output output;

            try
            {
                output = adapter.GetOutput(whichOutputDevice);
            }
            catch (SharpDXException)
            {
                throw new DesktopDuplicationException("Could not find the specified output device.");
            }
            outputStream             = output.QueryInterface <Output1>();
            this.mOutputDescription  = output.Description;
            this.mTextureDescription = new Texture2DDescription()
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = this.mOutputDescription.DesktopBounds.Right,
                Height            = this.mOutputDescription.DesktopBounds.Bottom,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = ResourceUsage.Staging
            };

            try
            {
                this.mDeskDuplication = outputStream.DuplicateOutput(mDevice);
            }
            catch (SharpDXException err)
            {
                if (err.ResultCode.Code == SharpDX.DXGI.ResultCode.NotCurrentlyAvailable.Result.Code)
                {
                    throw new DesktopDuplicationException("There is already the maximum number of applications using the Desktop Duplication API running, please close one of the applications and try again.");
                }
            }
        }
Exemplo n.º 20
0
        public void Init()
        {
            Dispose();

            factory = new Factory1();

            //Get first adapter
            adapter = factory.Adapters1.FirstOrDefault(x => x.Outputs.Length > 0);
            //Get device from adapter
            device = new SharpDX.Direct3D11.Device(adapter);
            //Get front buffer of the adapter
            if (adapter.GetOutputCount() < SelectedScreen + 1)
            {
                SelectedScreen = 0;
            }
            output  = adapter.GetOutput(SelectedScreen);
            output1 = output.QueryInterface <Output1>();

            // Width/Height of desktop to capture
            var bounds    = output1.Description.DesktopBounds;
            var newWidth  = bounds.Right - bounds.Left;
            var newHeight = bounds.Bottom - bounds.Top;

            CurrentScreenBounds = new Rectangle(bounds.Left, bounds.Top, newWidth, newHeight);
            if (newWidth != width || newHeight != height)
            {
                ScreenChanged?.Invoke(this, CurrentScreenBounds);
            }
            width  = newWidth;
            height = newHeight;

            CurrentFrame  = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            PreviousFrame = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            // Create Staging texture CPU-accessible
            textureDesc = new Texture2DDescription
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = ResourceUsage.Staging
            };
            screenTexture    = new Texture2D(device, textureDesc);
            duplicatedOutput = output1.DuplicateOutput(device);

            NeedsInit = false;
        }
Exemplo n.º 21
0
 public DirectXOutput(Adapter1 adapter,
                      SharpDX.Direct3D11.Device device,
                      OutputDuplication outputDuplication,
                      Texture2D texture2D,
                      DisplayModeRotation rotation)
 {
     Adapter           = adapter;
     Device            = device;
     OutputDuplication = outputDuplication;
     Texture2D         = texture2D;
     Rotation          = rotation;
 }
        public DXGIOutputDuplication(Adapter adapter,
                                     SharpDX.Direct3D11.Device device,
                                     OutputDuplication outputDuplication, OutputDescription description)
        {
            _adapter = adapter;

            _device        = device;
            _deviceContext = _device.ImmediateContext;

            _outputDuplication = outputDuplication;

            _description = description;
        }
Exemplo n.º 23
0
        public void Dispose()
        {
            Program.WriteLine("Begin to dispose DesktopDuplicationService");

            _desktopImageTexture?.Dispose();
            _device?.Dispose();
            _deskDupl?.Dispose();

            //important because of null checks
            _deskDupl            = null;
            _device              = null;
            _desktopImageTexture = null;

            Program.WriteLine("DesktopDuplicationService disposed");
        }
Exemplo n.º 24
0
        public DirectScreenshot(GPGPU gpu, int screenWidth, int screenHeight)
        {
            OutputFileName = "ScreenCapture.bmp";
            const int numAdapter = 0;
            const int numOutput  = 0;

            _gpu      = gpu;
            rgbValues = gpu.Allocate <GPUColorBGRA>(screenWidth * screenHeight);
            // Create DXGI Factory1
            factory = new Factory1();
            adapter = factory.GetAdapter1(numAdapter);

            // Create device from Adapter
            device = new Device(adapter);

            // Get DXGI.Output
            output  = adapter.GetOutput(numOutput);
            output1 = output.QueryInterface <Output1>();

            // Width/Height of desktop to capture
            width  = screenWidth;
            height = screenHeight;


            // Create Staging texture CPU-accessible
            textureDesc = new Texture2DDescription
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = ResourceUsage.Staging
            };
            screenTexture = new Texture2D(device, textureDesc);

            // Duplicate the output
            duplicatedOutput = output1.DuplicateOutput(device);

            var screenResource = BeginCapture(out var pp);

            screenResource.Dispose();
            duplicatedOutput.ReleaseFrame();
        }
Exemplo n.º 25
0
        /// <summary>
        /// Init some variables one times to spend execution time.
        /// </summary>
        public DirectX()
        {
            try
            {
                factory = new Factory1();
                adapter = factory.GetAdapter1(numAdapter);
                device  = new Device(adapter);
                output  = adapter.GetOutput(numOutput);
                output1 = output.QueryInterface <Output1>();
                // get screen wize

                textureDesc = new Texture2DDescription
                {
                    CpuAccessFlags    = CpuAccessFlags.Read,
                    BindFlags         = BindFlags.None,
                    Format            = Format.B8G8R8A8_UNorm,
                    Width             = ((SharpDX.Mathematics.Interop.RawRectangle)output.Description.DesktopBounds).Right - ((SharpDX.Mathematics.Interop.RawRectangle)output.Description.DesktopBounds).Left,
                    Height            = ((SharpDX.Mathematics.Interop.RawRectangle)output.Description.DesktopBounds).Bottom - ((SharpDX.Mathematics.Interop.RawRectangle)output.Description.DesktopBounds).Top,
                    OptionFlags       = ResourceOptionFlags.None,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Staging
                };

                screenTexture = new Texture2D(device, textureDesc);
                try
                {
                    duplicatedOutput = output1.DuplicateOutput(device);
                }
                catch (SharpDXException e)
                {
                    if (e.ResultCode.Code == SharpDX.DXGI.ResultCode.Unsupported.Result.Code)
                    {
                        throw new System.ApplicationException("Your system does not support DirectX 11.2 (normally on windows 7). Please use 'Use GDI Capture' option to prevent this error!");
                    }
                    else
                    {
                        throw e;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 26
0
 private void Reset()
 {
     using (Factory1 DXGIfactory = new Factory1())
     {
         Adapter1 adapter;
         if (DXGIfactory.Adapters1[0].Outputs.Length == 0)
         {
             adapter = DXGIfactory.Adapters1[1];
         }
         else
         {
             adapter = DXGIfactory.Adapters1[0];
         }
         Output1 output = adapter.Outputs[0].QueryInterface <Output1>();
         this.duplicatedOutput = output.DuplicateOutput(this.device);
     }
 }
Exemplo n.º 27
0
        /*Multi-Thread Variable*/

        /// <summary>
        /// Create LocalCapture to capture screen (by DXGI.)
        /// </summary>
        /// <param name="bitmapBuffer">Communication pipe with other threads. It stores some processing Mat and some unused Mat</param>
        /// <param name="numOutput"># of output device (i.e. monitor).</param>
        /// <param name="numAdapter"># of output adapter (i.e. iGPU). </param>
        public LocalCapture(BitmapBuffer bitmapBuffer, int numOutput = 0, int numAdapter = 0)
        {
            // Create DXGI Factory1
            factory = new Factory1();
            adapter = factory.GetAdapter1(numAdapter);

            // Create device from Adapter
            device = new Device(adapter);

            // Get DXGI.Output
            output  = adapter.GetOutput(numOutput);
            output1 = output.QueryInterface <Output1>();


            // Width/Height of desktop to capture
            width  = ((Rectangle)output.Description.DesktopBounds).Width;
            height = ((Rectangle)output.Description.DesktopBounds).Height;

            // Create Staging texture CPU-accessible
            textureDesc = new Texture2DDescription
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = ResourceUsage.Staging
            };
            screenTexture = new Texture2D(device, textureDesc);

            // Duplicate the output
            duplicatedOutput = output1.DuplicateOutput(device);

            //Create enough UnusedMat
            for (int i = 0; i < PreCreateMatCount; ++i)
            {
                bitmapBuffer.PushUnusedMat(CreateSuitableMat());
            }

            //Save the buffer
            this.bitmapBuffer = bitmapBuffer;
        }
Exemplo n.º 28
0
        private void Init()
        {
            // # of graphics card adapter
            const int numAdapter = 0;

            // # of output device (i.e. monitor)
            const int numOutput = 0;

            // Create DXGI Factory1
            var      factory = new Factory1();
            Adapter1 adapter = factory.GetAdapter1(numAdapter);

            // Create device from Adapter
            device = new Device(adapter);

            // Get DXGI.Output
            Output output  = adapter.GetOutput(numOutput);
            var    output1 = output.QueryInterface <Output1>();

            // Width/Height of desktop to capture
            width  = output.Description.DesktopBounds.Width;
            height = output.Description.DesktopBounds.Height;

            // Create Staging screenTexture CPU-accessible
            textureDesc = new Texture2DDescription
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = ResourceUsage.Staging
            };

            outputDescription = output.Description;

            // Demo the output
            duplicatedOutput = output1.DuplicateOutput(device);

            screenTexture = new Texture2D(device, textureDesc);
        }
Exemplo n.º 29
0
        public CompressScreen(bool _dxMode)
        {
            this.screenBounds = Screen.PrimaryScreen.Bounds;

            prev = new Bitmap(screenBounds.Width, screenBounds.Height, PixelFormat.Format32bppArgb);
            cur  = new Bitmap(screenBounds.Width, screenBounds.Height, PixelFormat.Format32bppArgb);

            using (Graphics g = Graphics.FromImage(prev))
            {
                g.Clear(Color.Black);
            }

            compressionBuffer = new byte[screenBounds.Width * screenBounds.Height * 4];

            int backBufSize = LZ4.LZ4Codec.MaximumOutputLength(compressionBuffer.Length) + 4;

            compressedScreen = new CompressedScreen(backBufSize);

            //directX Constructor

            if (_dxMode == true)
            {
                factory     = new Factory1();
                adapter     = factory.GetAdapter1(0);
                device      = new SharpDX.Direct3D11.Device(adapter);
                output      = adapter.GetOutput(0);
                output1     = output.QueryInterface <Output1>();
                textureDesc = new Texture2DDescription
                {
                    CpuAccessFlags    = CpuAccessFlags.Read,
                    BindFlags         = BindFlags.None,
                    Format            = Format.B8G8R8A8_UNorm,
                    Width             = screenBounds.Width,
                    Height            = screenBounds.Height,
                    OptionFlags       = ResourceOptionFlags.None,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage             = ResourceUsage.Staging
                };
                screenTexture    = new Texture2D(device, textureDesc);
                duplicatedOutput = output1.DuplicateOutput(device);
            }
        }
Exemplo n.º 30
0
            public DXSnapperInput(Factory1 factory, int adapterIndex, int outputIndex, Rectangle captureRectangle)
            {
                _adapter = factory.GetAdapter1(adapterIndex);
                _device  = new SharpDX.Direct3D11.Device(_adapter);
                _output  = _adapter.GetOutput(outputIndex);
                var outputBounds = _output.Description.DesktopBounds.ToGDIRect();

                var intersection = Rectangle.Intersect(outputBounds, captureRectangle);

                if (intersection.IsEmpty)
                {
                    Dispose();
                    throw new ArgumentOutOfRangeException(nameof(captureRectangle), $"Output {outputIndex} for adapter {adapterIndex} {FormatRectangle(outputBounds)} doesn't intersect with capture rectangle {FormatRectangle(captureRectangle)}");
                }

                _textureDescription = new Texture2DDescription
                {
                    CpuAccessFlags    = CpuAccessFlags.Read,
                    BindFlags         = BindFlags.None,
                    Format            = sourcePixelFormat,
                    Height            = outputBounds.Height,
                    Width             = outputBounds.Width,
                    OptionFlags       = ResourceOptionFlags.None,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    SampleDescription =
                    {
                        Count   = 1,
                        Quality = 0
                    },
                    Usage = ResourceUsage.Staging,
                };
                _screenTexture    = new Texture2D(_device, _textureDescription);
                _output1          = _output.QueryInterface <Output1>();
                _duplicatedOutput = _output1.DuplicateOutput(_device);

                _destXOffset   = intersection.Left - captureRectangle.Left;
                _destYOffset   = intersection.Top - captureRectangle.Top;
                _sourceXOffset = intersection.Left - outputBounds.Left;
                _sourceYOffset = intersection.Top - outputBounds.Top;
                _width         = intersection.Width;
                _height        = intersection.Height;
            }