Exemple #1
0
        public static void HandleGetDesktop(Packets.ServerPackets.GetDesktop command, Client client)
        {
            if (StreamCodec == null || StreamCodec.ImageQuality != command.Quality ||
                StreamCodec.Monitor != command.Monitor)
                StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor);

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

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

                StreamCodec = null;
            }
            finally
            {
                if (LastDesktopScreenshot != null)
                {
                    if (bmpdata != null)
                    {
                        LastDesktopScreenshot.UnlockBits(bmpdata);
                    }
                    LastDesktopScreenshot.Dispose();
                }
            }
        }
        public static void HandleGetDesktop(Packets.ServerPackets.GetDesktop command, Client client)
        {
            if (command.Action == RemoteDesktopAction.Stop)
            {
                IsStreamingDesktop = false;
                return;
            }

            if (IsStreamingDesktop) return;

            IsStreamingDesktop = true;

            if (StreamCodec == null || StreamCodec.ImageQuality != command.Quality || StreamCodec.Monitor != command.Monitor)
                StreamCodec = new UnsafeStreamCodec(command.Quality, command.Monitor);

            new Thread(() =>
            {
                while (IsStreamingDesktop)
                {
                    if (!Program.ConnectClient.Connected) // disconnected
                    {
                        IsStreamingDesktop = false;
                        return;
                    }
                    BitmapData desktopData = null;
                    Bitmap desktop = null;
                    try
                    {
                        desktop = Helper.Helper.GetDesktop(command.Monitor);
                        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);
                            new Packets.ClientPackets.GetDesktopResponse(stream.ToArray(), StreamCodec.ImageQuality,
                                StreamCodec.Monitor).Execute(client);
                        }
                    }
                    catch (Exception)
                    {
                        if (StreamCodec != null)
                            new Packets.ClientPackets.GetDesktopResponse(null, StreamCodec.ImageQuality, StreamCodec.Monitor).Execute(client);

                        StreamCodec = null;
                    }
                    finally
                    {
                        if (desktop != null)
                        {
                            if (desktopData != null)
                            {
                                desktop.UnlockBits(desktopData);
                            }
                            desktop.Dispose();
                        }
                    }
                }
            }).Start();
        }