private void CapturedDeviceSelectedIndexChanged(object sender, EventArgs e) { StartStop.Enabled = true; if (CapturedDevice.SelectedItem.ToString() != String.Empty) { CapturedMonitor.Enabled = true; } CapturedMonitor.Items.Clear(); foreach (SharpDX.DXGI.Output outputs in new SharpDX.DXGI.Factory1().Adapters[CapturedDevice.SelectedIndex].Outputs) { CapturedMonitor.Items.Add(outputs.Description.DeviceName.Trim('\\', '.')); } if (System.Configuration.ConfigurationManager.AppSettings["CapturedMonitor"] != null && CapturedMonitor.Items.Contains(System.Configuration.ConfigurationManager.AppSettings["CapturedMonitor"])) { CapturedMonitor.SelectedIndex = CapturedMonitor.FindString(System.Configuration.ConfigurationManager.AppSettings["CapturedMonitor"]); } else if (CapturedMonitor.Items.Count > 0) { CapturedMonitor.SelectedIndex = 0; } else { CapturedMonitor.Enabled = StartStop.Enabled = false; } }
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(); }