/// <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)
        {
            this.mWhichOutputDevice = whichOutputDevice;
            Adapter1 adapter = null;
            try
            {
                adapter = new Factory1().GetAdapter1(whichGraphicsCardAdapter);
            }
            catch (SharpDXException)
            {
                throw new DesktopDuplicationException("Could not find the specified graphics card adapter.");
            }
            this.mDevice = new Device(adapter);
            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>();
            this.mOutputDesc = output.Description;
            this.mTextureDesc = new Texture2DDescription()
            {
                CpuAccessFlags = CpuAccessFlags.Read,
                BindFlags = BindFlags.None,
                Format = Format.B8G8R8A8_UNorm,
                Width = this.mOutputDesc.DesktopBounds.Width,
                Height = this.mOutputDesc.DesktopBounds.Height,
                OptionFlags = ResourceOptionFlags.None,
                MipLevels = 1,
                ArraySize = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage = ResourceUsage.Staging
            };

            try
            {
                this.mDeskDupl = output1.DuplicateOutput(mDevice);
            }
            catch (SharpDXException ex)
            {
                if (ex.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.");
                }
            }
        }
Beispiel #2
0
        public ScreenDublicator()
        {
            Adapter adapter = null;
            Output output = null;
            Output1 output1 = null;
            try
            { 
                //Адаптер
                
                adapter = new Factory1().GetAdapter1(this.ADAPTER);
            
                //Устройство

                this._device = new Device(adapter);
                output = adapter.GetOutput(this.MONITOR);
            
                //Выход

                output1 = output.QueryInterface<Output1>();
                this._outputDesc = output.Description;
                this._width = this._outputDesc.DesktopBounds.Right - this._outputDesc.DesktopBounds.Left;
                this._height = this._outputDesc.DesktopBounds.Bottom - this._outputDesc.DesktopBounds.Top;
                this._textureDesc = new Texture2DDescription()
                {
                    CpuAccessFlags = CpuAccessFlags.Read,
                    BindFlags = BindFlags.None,
                    Format = Format.B8G8R8A8_UNorm,
                    Width = this._width,
                    Height = this._height,
                    OptionFlags = ResourceOptionFlags.None,
                    MipLevels = 1,
                    ArraySize = 1,
                    SampleDescription = { Count = 1, Quality = 0 },
                    Usage = ResourceUsage.Staging
                };
                this._outputDublication = output1.DuplicateOutput(this._device);

                //Текстура

                this._desktopTexture = new Texture2D(this._device, this._textureDesc);
            }
            catch(SharpDXException sdxex)
            {
                if (sdxex.ResultCode.Code == SharpDX.DXGI.ResultCode.NotCurrentlyAvailable.Result.Code)
                {
                    throw new ScreenDublicatorException("Достигнут предел приложений, работающих с API)");
                }
                else
                {
                    string message = String.Format("Не удалось создать дублирующий выход");
                    Debug.WriteLine(message + "\n{0}\n{1}", sdxex.Message, sdxex.StackTrace);
                    throw new ScreenDublicatorException(message);
                }
            }
            catch(Exception ex)
            {
                string message = String.Format("Ошибка при инициализации объекта\n{0}\n{1}", ex.Message, ex.StackTrace);
                throw new ScreenDublicatorException(message);
            }
            finally
            {
                if(adapter != null) { adapter.Dispose(); }
                if(output != null) { output.Dispose(); }
                if(output1 != null) { output1.Dispose(); }
            }
        }