You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.6 KiB

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Client
{
class Client
{
private readonly int BUFF_SIZE = 1024;
private string IP { get; set; } = "127.0.0.1";
private int Port { get; set; } = 7000;
public Client(string ip = "127.0.0.1", int port = 7000)
{
this.IP = ip;
this.Port = port;
}
public byte Connect()
{
TcpClient client = new TcpClient(this.IP, this.Port);
Bitmap bmp = CaptureScreen();
ImageConverter imgConverter = new ImageConverter();
byte[] imgBytes = (byte[])imgConverter.ConvertTo(bmp, typeof(byte[]));
byte[] nBytes = BitConverter.GetBytes(imgBytes.Length);
byte[] result = new byte[1];
using (NetworkStream stream = client.GetStream())
{
// Send data size
stream.Write(nBytes, 0, nBytes.Length);
// Send image
int end = imgBytes.Length;
int start = 0;
while (start < end)
{
int size = end - start >= BUFF_SIZE ? BUFF_SIZE : end - start;
stream.Write(imgBytes, start, size);
start += size;
}
// Receive result
result = new byte[1];
stream.Read(result, 0, result.Length);
Console.WriteLine(result[0]);
}
client.Close();
return result[0];
}
private Bitmap CaptureScreen()
{
Rectangle rect = Screen.PrimaryScreen.Bounds;
Bitmap img = new Bitmap(rect.Width, rect.Height);
using (Graphics g = Graphics.FromImage(img))
{
g.CopyFromScreen(rect.X, rect.Y, 0, 0, img.Size, CopyPixelOperation.SourceCopy);
}
return img;
}
}
}