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); }
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); } }
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); }
/// <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) { _mWhichOutputDevice = whichOutputDevice; Adapter1 adapter; try { adapter = new Factory1().GetAdapter1(whichGraphicsCardAdapter); } catch (SharpDXException) { throw new DesktopDuplicationException("Could not find the specified graphics card adapter."); } _mDevice = new Device(adapter); Output output; 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 = ((Rectangle)_mOutputDesc.DesktopBounds).Width, Height = ((Rectangle)_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."); } } }
/// <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."); } } }
/// <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) { Adapter1 adapter; try { adapter = new Factory1().GetAdapter1(whichGraphicsCardAdapter); } catch (SharpDXException ex) { throw new DesktopDuplicationException("Could not find the specified graphics card adapter.", ex); } _device = new Device(adapter); Output output; try { output = adapter.GetOutput(whichOutputDevice); } catch (SharpDXException ex) { throw new DesktopDuplicationException("Could not find the specified output device.", ex); } var output1 = output.QueryInterface <Output1>(); _outputDescription = output.Description; try { _outputDuplication = output1.DuplicateOutput(_device); } 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."); } } }
public override void Init() { base.Init(); var adapter = new Factory1().GetAdapter1(0); m_Device = new SharpDX.Direct3D11.Device(adapter); var output = adapter.GetOutput(0); m_Output1 = output.QueryInterface <Output1>(); Width = output.Description.DesktopBounds.Right; Height = output.Description.DesktopBounds.Bottom; /* * m_DuplicatedOutput = m_Output1.DuplicateOutput(m_Device); * * var 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 * }; * m_ScreenTexture = new Texture2D(m_Device, textureDesc); */ m_ScreenBmp = new Bitmap(Width, Height, PixelFormat.Format32bppArgb); m_TempBitmap = new Bitmap(Width, Height, PixelFormat.Format32bppArgb); Initialized?.Invoke(); }
private void DXCapture() { Adapter adapter = null; CapturedDevice.Invoke((MethodInvoker) delegate { adapter = new Factory1().Adapters[CapturedDevice.SelectedIndex]; }); //Console.WriteLine(factory.GetAdapterCount()); var device = new Device(adapter); //Create device from Adapter //foreach (Adapter adapters in factory.Adapters) Console.WriteLine(adapters.Description.Description); Output output = null; //Get DXGI.Output //foreach (Output outputs in adapter.Outputs) Console.WriteLine(outputs.Description.DeviceName); CapturedMonitor.Invoke((MethodInvoker) delegate { output = adapter.GetOutput(CapturedMonitor.SelectedIndex); }); var output1 = output.QueryInterface <Output1>(); int width = output.Description.DesktopBounds.Right; //Width/Height of desktop to capture int height = output.Description.DesktopBounds.Bottom; Texture2DDescription textureDesc = new Texture2DDescription //Create Staging texture CPU-accessible { 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 }; var screenTexture = new Texture2D(device, textureDesc); var duplicatedOutput = output1.DuplicateOutput(device); //Duplicate the output int ledsx = LedsX.Value; int ledsy = LedsY.Value; int leftoffset = 0, upperoffset = 0, customwidth = width, customheight = height; int index = 0; CaptureArea.Invoke((MethodInvoker) delegate { index = CaptureArea.SelectedIndex; }); if (index == 1) { leftoffset = (width - Convert.ToInt32(CustomWidth.Text)) / 2; upperoffset = (height - Convert.ToInt32(CustomHeight.Text)) / 2; customwidth = Convert.ToInt32(CustomWidth.Text); customheight = Convert.ToInt32(CustomHeight.Text); } else if (index == 2) { leftoffset = Convert.ToInt32(LeftOffset.Text); upperoffset = Convert.ToInt32(UpperOffset.Text); customwidth = width - leftoffset - Convert.ToInt32(RightOffset.Text); customheight = height - upperoffset - Convert.ToInt32(LowerOffset.Text); } bool init = false; sw.Start(); Task.Factory.StartNew(() => { while (StartStop.Checked && SerialPort.IsOpen) { WakeUp(); try { duplicatedOutput.AcquireNextFrame(100, out OutputDuplicateFrameInformation duplicateFrameInformation, out SharpDX.DXGI.Resource screenResource); // Try to get duplicated frame within given time if (init) { using (var screenTexture2D = screenResource.QueryInterface <Texture2D>()) device.ImmediateContext.CopyResource(screenTexture2D, screenTexture); // copy resource into memory that can be accessed by the CPU var mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, MapFlags.None); // Get the desktop capture texture var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); // Create Drawing.Bitmap var boundsRect = new Rectangle(0, 0, width, height); var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat); // Copy pixels from screen capture Texture to GDI bitmap var sourcePtr = mapSource.DataPointer; var destPtr = mapDest.Scan0; for (int y = 0; y < height; y++) { Utilities.CopyMemory(destPtr, sourcePtr, width * 4); // Copy a single line sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch); // Advance pointers destPtr = IntPtr.Add(destPtr, mapDest.Stride); } bitmap.UnlockBits(mapDest); // Release source and dest locks device.ImmediateContext.UnmapSubresource(screenTexture, 0); using (var tempbmp = new Bitmap(ledsx, ledsy, PixelFormat.Format32bppRgb)) { Graphics tempbmpgr = Graphics.FromImage(tempbmp); tempbmpgr.InterpolationMode = intrpmode; //nearest nighbour, low, default, bicubic, bilinear, tempbmpgr.DrawImage(bitmap, new Rectangle(0, 0, ledsx, ledsy), leftoffset, upperoffset, customwidth, customheight, GraphicsUnit.Pixel); Color color; for (int x = 0; x < ledsx; x++) //these for's take 5-7ms { color = tempbmp.GetPixel(x, ledsy - 1); SerialPort.Write(new byte[] { color.R, color.G, color.B }, 0, 3); } for (int y = ledsy - 1; y >= 0; y--) { color = tempbmp.GetPixel(ledsx - 1, y); SerialPort.Write(new byte[] { color.R, color.G, color.B }, 0, 3); } for (int x = ledsx - 1; x >= 0; x--) { color = tempbmp.GetPixel(x, 0); SerialPort.Write(new byte[] { color.R, color.G, color.B }, 0, 3); } for (int y = 0; y < ledsy; y++) { color = tempbmp.GetPixel(0, y); SerialPort.Write(new byte[] { color.R, color.G, color.B }, 0, 3); } ScreenRefreshed?.Invoke(this, EventArgs.Empty); GC.Collect(); } } init = true; screenResource.Dispose(); duplicatedOutput.ReleaseFrame(); } catch (SharpDXException) { } } if (!StartStop.Checked) { sw.Stop(); SerialPort.Close(); duplicatedOutput.Dispose(); screenTexture.Dispose(); } }); GC.Collect(); }
/// <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) { //SharpDX.Configuration.EnableObjectTracking = true; 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.GetWidth(), Height = this.mOutputDesc.DesktopBounds.GetHeight(), 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) { output.Dispose(); output1.Dispose(); adapter.Dispose(); 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."); } } output.Dispose(); output1.Dispose(); adapter.Dispose(); }
/// <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."); } } /* * var t = new Thread(() => * { * Action Allocate1K = () => * { * for (var i = 0; i < 1500; i++) * { * var newBitmap = * new System.Drawing.Bitmap(1920, 1200, PixelFormat.Format32bppRgb); * * _allocatedBitmaps.Enqueue(newBitmap); * * if (i % 10 == 0) Thread.Sleep(1); * } * }; * * while (true) * { * if (_allocatedBitmaps.Count < 500) * { * Allocate1K(); * } * else * { * Thread.Sleep(1000); * } * } * }); * t.Priority = ThreadPriority.Lowest; * t.Start(); */ }
public void Initialize(int monitor) { const int graphicsCardAdapter = 0; Adapter1 adapter; try { adapter = new Factory1().GetAdapter1(graphicsCardAdapter); } catch (SharpDXException) { throw new DesktopDuplicationException("Could not find the specified graphics card adapter."); } Output output; using (adapter) { _device = new Device(adapter); try { output = adapter.GetOutput(monitor); } catch (SharpDXException) { throw new DesktopDuplicationException("Could not find the specified output device."); } } using (output) using (var output1 = output.QueryInterface <Output1>()) { _outputDesc = output.Description; _textureDesc = new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.Read, BindFlags = BindFlags.None, Format = Format.B8G8R8A8_UNorm, Width = _outputDesc.DesktopBounds.GetWidth(), Height = _outputDesc.DesktopBounds.GetHeight(), OptionFlags = ResourceOptionFlags.None, MipLevels = 1, ArraySize = 1, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Staging }; try { _deskDupl = output1.DuplicateOutput(_device); } 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."); } } } _currentMonitor = monitor; _screenHelper = new ScreenHelper(); }