0

我在通过 c# 中的套接字发送屏幕截图时遇到问题。

客户

 private void btnCaptureScreen_Click(object sender, EventArgs e)
    {
        clientSocket = new TcpClient();
        clientSocket.Connect(txtIP.Text, 8888);

        NetworkStream serverStream = clientSocket.GetStream();

        byte[] inStream = new byte[9999999];
        serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
        ImageConverter ic = new ImageConverter();
        Image img = (Image)ic.ConvertFrom(inStream);
        Bitmap bit = new Bitmap(img);
        bit.Save(@"C:\temp\capturas\scn" + numCapturas + ".png", ImageFormat.Png);
        clientSocket.Close();
    }

服务器

    TcpListener serverSocket = new TcpListener(IPAddress.Parse(LocalIPAddress()), 8888);

    TcpClient clientSocket = default(TcpClient);
    serverSocket.Start();
    NetworkStream networkStream = clientSocket.GetStream();
    Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,  Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
    Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot);

    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

    ImageConverter converter = new ImageConverter();
            Byte[] sendBytes = (byte[])converter.ConvertTo(bmpScreenshot, typeof(byte[]));
    networkStream.Write(sendBytes, 0, sendBytes.Length);
    networkStream.Flush();

这是我的代码的一部分。如果我将服务器中的屏幕截图保存到一个文件夹中,它可以正常工作,但是当我通过套接字将它发送到客户端后,客户端只显示图像的一半,图像的另一半不正确,因为它都是灰色的。

谢谢!

编辑:现在我尝试改进阅读逻辑......但现在不起作用,当我尝试保存位图时出现 ArgumentException。

客户

clientSocket = new TcpClient(); clientSocket.Connect(txtIP.Text, 8888);

    NetworkStream serverStream = clientSocket.GetStream();

    byte[] outStream = System.Text.Encoding.ASCII.GetBytes("screenCapture()$");

    serverStream.Write(outStream, 0, outStream.Length);
    serverStream.Flush();

    byte[] bytesFrom = new byte[10025];
    serverStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);


    String dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
    Int64 lengthdata = Convert.ToInt64(dataFromClient);
    byte[] inStream = new byte[lengthdata];

    int recived = 0;
   while (recived != lengthdata)
    {
        recived += serverStream.Read(inStream, 0,(int)clientSocket.ReceiveBufferSize);
    }

    TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
    Bitmap screenShot = (Bitmap)tc.ConvertFrom(inStream); //<--- Exception 
    screenShot.Save(@"C:\temp\capturas\scn" + numCapturas + ".png", ImageFormat.Png);
    clientSocket.Close();

服务器:

private void sendScreenCapture(NetworkStream networkStream)
{
    Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
    Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot);

    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

    ImageConverter converter = new ImageConverter();
    byte[] sendBytes = (byte[])converter.ConvertTo(bmpScreenshot, typeof(byte[]));

    byte[] tamañoImagen = System.Text.Encoding.ASCII.GetBytes(sendBytes.Length.ToString() + "$");
    networkStream.Write(tamañoImagen, 0, tamañoImagen.Length);


    networkStream.Write(sendBytes, 0, sendBytes.Length);
    networkStream.Flush();
}

编辑2:哈哈终于明白了,我必须控制字节[]中的位置:

  int pos = 0;
   while (lengthdata > 0)
   {
     int recived = serverStream.Read(inStream, pos, (int)lengthdata);
     if (recived == 0) break;
     lengthdata -= recived;
     pos+=recived;
   }
4

1 回答 1

2

您正在读取clientSocket.ReceiveBufferSize一次字节并停止。据推测,图像更大,所以你错过了它的一部分。你需要更好的阅读逻辑......

一般来说,听起来您正在尝试自制远程桌面实现 - 考虑使用实际的远程桌面或类似工具。

于 2013-04-02T10:50:32.403 回答