}//GDIScreenShot public override void Start() { this.Run = true; int width = SystemInformation.VirtualScreen.Width; int height = SystemInformation.VirtualScreen.Height; Bitmap screenBmp = null; Graphics bmpGraphics = null; if (this.OnlyWindowScreen == false) { screenBmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); bmpGraphics = Graphics.FromImage(screenBmp); }//if IntPtr activeWindow = IntPtr.Zero; //IntPtr hdcTo = IntPtr.Zero; //IntPtr hdcFrom = IntPtr.Zero; IntPtr hBitmap = IntPtr.Zero; this.screenThread = new Thread(() => { Start: try { while (this.Run) { try { if (this.OnlyWindowScreen) { activeWindow = WIN32.GetForegroundWindow(); //hdcFrom = WIN32.GetDC(activeWindow); //hdcFrom = WIN32.GetWindowDC(activeWindow); var r = new WIN32.Rect(); if (WIN32.GetWindowRect(activeWindow, ref r)) { int w = r.right - r.left; int h = r.bottom - r.top; uint processID = 0; WIN32.GetWindowThreadProcessId(activeWindow, out processID); var p = Process.GetProcessById((int)processID); //Program.ConsoleWriteLine($"{p.ProcessName}", Program.Red, Color.White); screenBmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb); bmpGraphics = Graphics.FromImage(screenBmp); if (p.ProcessName == "chrome") { bmpGraphics.CopyFromScreen(r.left, r.top, 0, 0, new System.Drawing.Size(w, h)); }//if else { hBitmap = bmpGraphics.GetHdc(); WIN32.PrintWindow(activeWindow, hBitmap, 0); bmpGraphics.ReleaseHdc(); }//else bmpGraphics.Dispose(); }//if else { continue; } //if (hdcFrom != IntPtr.Zero) // WIN32.ReleaseDC(activeWindow, hdcFrom); //if (hdcTo != IntPtr.Zero) // WIN32.DeleteDC(hdcTo); if (hBitmap != IntPtr.Zero) { WIN32.DeleteObject(hBitmap); } }//if else { //Рисование скриншота экрана в screenBmp (баз мыши) IntPtr dc1 = WIN32.GetDC(IntPtr.Zero); IntPtr dc2 = bmpGraphics.GetHdc(); WIN32.BitBlt(dc2, 0, 0, width, height, dc1, 0, 0, 13369376); WIN32.ReleaseDC(IntPtr.Zero, dc1); bmpGraphics.ReleaseHdc(dc2); WIN32.CURSORINFO ci = new WIN32.CURSORINFO(); ci.cbSize = Marshal.SizeOf(typeof(WIN32.CURSORINFO)); WIN32.GetCursorInfo(ref ci); if (System.Windows.Forms.Cursor.Current != null) { using (Icon cursorIcon = System.Drawing.Icon.FromHandle(ci.hCursor)) bmpGraphics.DrawIcon(cursorIcon, new System.Drawing.Rectangle(System.Windows.Forms.Cursor.Position, cursorIcon.Size)); } //if } //else } //try catch (ThreadAbortException tax) { return; }//catch catch { //if (hdcFrom != IntPtr.Zero) // WIN32.ReleaseDC(activeWindow, hdcFrom); //if (hdcTo != IntPtr.Zero) // WIN32.DeleteDC(hdcTo); if (hBitmap != IntPtr.Zero) { WIN32.DeleteObject(hBitmap); } }//catch //Конвертация Bitmap изображение в байты (FullHD 120000-300000 байт обычно) (Jpeg) using (var ms = new MemoryStream()) { byte[] temp = new byte[MaxLength]; screenBmp.Save(ms, ImageFormat.Jpeg); ScreenRefreshed?.Invoke(this, this.MSToByteArrays(ms)); } //using } //while } //try catch (ThreadAbortException tax) { return; }//catch catch (Exception ex) { Program.ConsoleWriteLine(ex.Message, Program.Red, Color.White); using (var ms = new MemoryStream()) { var b = new Bitmap(width, height, PixelFormat.Format32bppArgb); b.Save(ms, ImageFormat.Jpeg); ScreenRefreshed?.Invoke(this, this.MSToByteArrays(ms)); b.Dispose(); }//using goto Start; }//catch }); screenThread.IsBackground = true; screenThread.Priority = ThreadPriority.Highest; screenThread.Start(); } //Start
public void Start() { _run = true; var factory = new Factory1(); //Get first adapter var adapter = factory.GetAdapter1(0); //Get device from adapter var device = new SharpDX.Direct3D11.Device(adapter); //Get front buffer of the adapter var output = adapter.GetOutput(0); var output1 = output.QueryInterface <Output1>(); // Width/Height of desktop to capture int width = output.Description.DesktopBounds.Right; int height = output.Description.DesktopBounds.Bottom; // Create Staging texture CPU-accessible 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 }; var screenTexture = new Texture2D(device, textureDesc); Task.Factory.StartNew(() => { // Duplicate the output using (var duplicatedOutput = output1.DuplicateOutput(device)) { while (_run) { try { SharpDX.DXGI.Resource screenResource; OutputDuplicateFrameInformation duplicateFrameInformation; // Try to get duplicated frame within given time is ms duplicatedOutput.AcquireNextFrame(10, out duplicateFrameInformation, out screenResource); // copy resource into memory that can be accessed by the CPU using (var screenTexture2D = screenResource.QueryInterface <Texture2D>()) device.ImmediateContext.CopyResource(screenTexture2D, screenTexture); // Get the desktop capture texture var mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None); // Create Drawing.Bitmap using (var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb)) { var boundsRect = new Rectangle(0, 0, width, height); // Copy pixels from screen capture Texture to GDI bitmap var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat); var sourcePtr = mapSource.DataPointer; var destPtr = mapDest.Scan0; for (int y = 0; y < height; y++) { // Copy a single line Utilities.CopyMemory(destPtr, sourcePtr, width * 4); // Advance pointers sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch); destPtr = IntPtr.Add(destPtr, mapDest.Stride); } // Release source and dest locks bitmap.UnlockBits(mapDest); device.ImmediateContext.UnmapSubresource(screenTexture, 0); using (var ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Bmp); ScreenRefreshed?.Invoke(this, ms.ToArray()); _init = true; } } screenResource.Dispose(); duplicatedOutput.ReleaseFrame(); } catch (SharpDXException e) { if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code) { Trace.TraceError(e.Message); Trace.TraceError(e.StackTrace); } } } } }); while (!_init) { ; } }
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(); }
public override void Start() { this.Run = true; var factory = new Factory1(); //Get first adapter var adapter = factory.GetAdapter1(0); //Get device from adapter var device = new SharpDX.Direct3D11.Device(adapter); //Get front buffer of the adapter var output = adapter.GetOutput(0); var output1 = output.QueryInterface <Output1>(); // Width/Height of desktop to capture int width = output.Description.DesktopBounds.Right; int height = output.Description.DesktopBounds.Bottom; // Create Staging texture CPU-accessible 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 }; var screenTexture = new Texture2D(device, textureDesc); var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); var boundsRect = new Rectangle(0, 0, width, height); var bmpGraphics = Graphics.FromImage(bitmap); this.screenThread = new Thread(() => { // Duplicate the output Start: try { using (var duplicatedOutput = output1.DuplicateOutput(device)) { while (this.Run) { try { SharpDX.DXGI.Resource screenResource; OutputDuplicateFrameInformation duplicateFrameInformation; // Try to get duplicated frame within given time is ms duplicatedOutput.AcquireNextFrame(5, out duplicateFrameInformation, out screenResource); // copy resource into memory that can be accessed by the CPU using (var screenTexture2D = screenResource.QueryInterface <Texture2D>()) device.ImmediateContext.CopyResource(screenTexture2D, screenTexture); // Get the desktop capture texture var mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None); // Create Drawing.Bitmap // Copy pixels from screen capture Texture to GDI bitmap var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat); var sourcePtr = mapSource.DataPointer; var destPtr = mapDest.Scan0; for (int y = 0; y < height; y++) { // Copy a single line Utilities.CopyMemory(destPtr, sourcePtr, width * 4); // Advance pointers sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch); destPtr = IntPtr.Add(destPtr, mapDest.Stride); }//for // Release source and dest locks bitmap.UnlockBits(mapDest); device.ImmediateContext.UnmapSubresource(screenTexture, 0); try { ////////////////////Рисование курсора///////////////////////////// WIN32.CURSORINFO ci = new WIN32.CURSORINFO(); ci.cbSize = Marshal.SizeOf(typeof(WIN32.CURSORINFO)); WIN32.GetCursorInfo(ref ci); if (System.Windows.Forms.Cursor.Current != null) { using (Icon cursorIcon = System.Drawing.Icon.FromHandle(ci.hCursor)) bmpGraphics.DrawIcon(cursorIcon, new System.Drawing.Rectangle(System.Windows.Forms.Cursor.Position, cursorIcon.Size)); }//if ////////////////////////////////////////////////////////////////// } //try catch { } //catch using (var ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Jpeg); ScreenRefreshed?.Invoke(this, this.MSToByteArrays(ms)); }//using screenResource.Dispose(); duplicatedOutput.ReleaseFrame(); }//try catch (SharpDXException sdx) { if (sdx.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code && sdx.ResultCode.Code != SharpDX.DXGI.ResultCode.AccessLost.Code && sdx.ResultCode.Code != SharpDX.DXGI.ResultCode.InvalidCall.Code) { Program.ConsoleWriteLine(sdx.Message, Program.Red, Color.White); using (var ms = new MemoryStream()) { var b = new Bitmap(width, height, PixelFormat.Format32bppArgb); b.Save(ms, ImageFormat.Jpeg); ScreenRefreshed?.Invoke(this, this.MSToByteArrays(ms)); b.Dispose(); }//using goto Start; } //if } //catch } //while } //using } //try catch (SharpDXException ex) { if (ex.ResultCode.Code == SharpDX.DXGI.ResultCode.Unsupported.Result.Code) { Thread.Sleep(200); Program.ConsoleWriteLine("DirectX11 UNSUPPORTED! Please Stop Server And Use GDI!", Program.Red, Color.White); return; }//if Program.ConsoleWriteLine(ex.Message, Program.Red, Color.White); goto Start; }//catch }); screenThread.IsBackground = true; screenThread.Priority = ThreadPriority.Highest; screenThread.Start(); } //Start
public void Start() { _run = true; var factory = new Factory1(); var adapter = factory.GetAdapter1(0); var device = new SharpDX.Direct3D11.Device(adapter); //Get front buffer of the adapter var output = adapter.GetOutput(0); var output1 = output.QueryInterface <Output1>(); // Screen resolution int width = output.Description.DesktopBounds.Right; int height = output.Description.DesktopBounds.Bottom; // Create Staging texture CPU - accessible 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 }; var screenTexture = new Texture2D(device, textureDesc); Task.Factory.StartNew(() => { using (var copy_Output = output1.DuplicateOutput(device)) { Stopwatch time; // Time counter, you can remove it. while (_run) { time = System.Diagnostics.Stopwatch.StartNew(); try { SharpDX.DXGI.Resource screenResource; OutputDuplicateFrameInformation copy_FrameInformation; copy_Output.AcquireNextFrame(5, out copy_FrameInformation, out screenResource); using (var screenTexture2D = screenResource.QueryInterface <Texture2D>()) device.ImmediateContext.CopyResource(screenTexture2D, screenTexture); var mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None); var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); var boundsRect = new System.Drawing.Rectangle(0, 0, width, height); var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat); var sourcePtr = mapSource.DataPointer; var destPtr = mapDest.Scan0; for (int y = 0; y < height; y++) { SharpDX.Utilities.CopyMemory(destPtr, sourcePtr, width * 4); sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch); destPtr = IntPtr.Add(destPtr, mapDest.Stride); } // Release source and dest locks bitmap.UnlockBits(mapDest); device.ImmediateContext.UnmapSubresource(screenTexture, 0); using (var ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Bmp); ScreenRefreshed?.Invoke(this, bitmap); //ScreenRefreshed?.Invoke(this, ms.ToArray()); //To send the image array _init = true; } screenResource.Dispose(); copy_Output.ReleaseFrame(); } catch (SharpDXException e) { // Some graphics cards could spam warnings for this. if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code) { Trace.TraceError(e.Message); Trace.TraceError(e.StackTrace); } } // Time, you can remove too. // Just add to a List the value of the time wasted to calculate the averrage. time.Stop(); form.last_times.Add(time.ElapsedMilliseconds); } } }); while (!_init) { ; } }