我正在用 c# 创建一个程序来记录屏幕并通过套接字连接将其发送到服务器。我的问题是我需要将其转换为字节才能发送。这是我来自客户端的代码,因此正在录制屏幕的计算机:
public Form1()
{
InitializeComponent();
}
static int port = 443;
static IPAddress IP;
static Socket server;
private Bitmap bm;
private string PCname = SystemInformation.ComputerName;
private string UserName = SystemInformation.UserName;
private void btnStart_Click(object sender, EventArgs e)
{
// Connect to server
IP = IPAddress.Parse("127.0.0.1");
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Connect(new IPEndPoint(IP, port));
// Record the screen
timer1.Start();
// Send screen to server
byte[] sdata = Encoding.Default.GetBytes(pictureBox1);
server.Send(sdata, 0, sdata.Length, 0);
}
private void timer1_Tick(object sender, EventArgs e)
{
// Take screenshot
bm = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bm as Image);
graphics.CopyFromScreen(0, 0, 0, 0, bm.Size);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
// Show it in picturebox
pictureBox1.Image = bm;
}
private void btnStop_Click(object sender, EventArgs e)
{
timer1.Stop();
server.Close();
}