Beispiel #1
0
        }//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 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