我编写这段代码是为了将屏幕截图发送到多个连接的客户端。在客户端上工作正常,但在服务器端冻结了应用程序的 UI。我不明白是什么导致了这个问题。
public void LoopClients()
{
while (_isRunning)
{
TcpClient newClient = Server.AcceptTcpClient();
Thread t = new Thread(new
ParameterizedThreadStart(HandleClient));
t.Start(newClient);
}
}
public void HandleClient(object obj)
{
TcpClient client = (TcpClient)obj;
BinaryFormatter binaryformatter = new BinaryFormatter();
while (client.Connected)
{
MainStream = client.GetStream();
binaryformatter.Serialize(MainStream, GrabDesktop());
}
}
private static Image GrabDesktop()
{
System.Drawing.Rectangle bound = Screen.PrimaryScreen.Bounds;
Bitmap screenshot = new Bitmap(bound.Width, bound.Height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(screenshot);
graphics.CopyFromScreen(bound.X, bound.Y, 0, 0, bound.Size, CopyPixelOperation.SourceCopy);
return screenshot;
}
任何改进代码或修复以解决问题的帮助或建议都会有很大帮助。
料青山看我应如是
回首忆惘然
相关分类