【问题标题】:Send and receive an image with a tcp socket使用 tcp 套接字发送和接收图像
【发布时间】:2014-07-31 01:07:43
【问题描述】:

我已经编写了这些代码来使用 TCP 套接字发送和接收图像,但接收代码不起作用,希望你能帮助我 这是发送代码

public void SendImage()
{
    int ScreenWidth = Screen.GetBounds(new Point(0, 0)).Width;
    int ScreenHeight = Screen.GetBounds(new Point(0, 0)).Height;
    Bitmap bmpScreenShot = new Bitmap(ScreenWidth, ScreenHeight);

    Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
    gfx.CopyFromScreen(0, 0, 0, 0, new Size(ScreenWidth, ScreenHeight));
    bmpScreenShot.Save(Application.StartupPath + "/ScreenShot.jpg", ImageFormat.Jpeg);
    byte[] image = new byte[1];
    bmpScreenShot = ResizeBitmap(bmpScreenShot, 300, 300);

    image = ImageToByte(bmpScreenShot);
    //get the length of image (length of bytes)
    int NumberOfBytes = image.Length;
    //put the size into a byte array
    byte[] numberofbytesArray = BitConverter.GetBytes(NumberOfBytes);

    //send the size to the Client
    int sizesend = sck.Send(numberofbytesArray, 0, numberofbytesArray.Length, 0);
    if (sizesend > 0)
    {
        MessageBox.Show("Size Sent");
    }
    //send the image to the Client
    int imagesend =sck.Send(image, 0, NumberOfBytes, 0);
    if (imagesend > 0)
    {
        MessageBox.Show("Image Sent");
    }
}

这是接收代码

public void ReceiveImage()
{
    if (sck.Connected)
    {
        NetworkStream stream = new NetworkStream(sck);
        byte[] data = new byte[4];

        //Read The Size
        stream.Read(data, 0, data.Length);
        int size = IPAdress.HostToNetworkOrder(BitConverter.ToInt32(data,0));
        // prepare buffer
        data = new byte[size];

        //Load Image
        int read = 0;
        while (read != data.Length)
        {
           read += stream.Read(data, read, data.Length - read);
        }
        //stream.Read(data, 0, data.Length);
        //Convert Image Data To Image
        MemoryStream imagestream = new MemoryStream(data);
        Bitmap bmp = new Bitmap(imagestream);
        pictureBox1.Image = bmp;                    
    }
}

编辑 删除 IPAdress.HostToNetworkOrder 到以下内容后

int size = BitConverter.ToInt32(data,0);

还是有问题。问题是当我发送大小时,它以 5kb 发送,但当我收到它时,我发现它更接近 2GB。

此外,我在这一行遇到错误

read += stream.Read(data, read, data.Length - read);

带有以下信息

无法从传输连接中读取数据。由于系统缺少足够的缓冲区空间或队列已满,可能会对套接字执行操作。

【问题讨论】:

  • 在您尝试执行data = new byte[size]; 行之前,size 中的值是多少?
  • -1991225785 是值,但是当我删除 IPAdress.HostToNetworkOrder 时它变为正数
  • 您似乎想要相反的 - IPAddress.NetworkToHostOrder,不是吗?您从网络中获取它,并且需要它作为当前主机上的 int。
  • 顺便说一句,您不必分配image,因为您将其分配给“ImageToByte”的结果,就在几行之后。尝试分配 1 000 000 000 个字节然后将其丢弃似乎是一种浪费......
  • 你的 ImageToByte() 函数在哪里?这似乎是有缺陷的,并且返回了比需要更多的字节。检查此 SO 链接,该链接显示如何将图像转换为字节,反之亦然:stackoverflow.com/questions/8764280/…

标签: c# image tcp


【解决方案1】:

不要使用 HostToNetwork 顺序,除非您正在创建一个应该与 java 服务器/客户端兼容的服务器。如果这样做,您还需要更改您发送的 int 数据缓冲区的顺序。

您还可以受益于将您在客户端收到的字节直接写入内存流,而不是分配数据并将字节写入内存流。还有一个重要的注意事项,在将其交给位图构造函数之前不要忘记设置imagestream.Position = 0。

【讨论】:

  • 好的,当我删除它时出现此错误`无法从传输连接读取数据。由于系统缺少足够的缓冲区空间或队列已满,因此可能会在套接字上执行操作。 `
  • @TarekAdel:它是从哪里“出现”的?当您使用 Read 函数单步执行代码行时?
  • 我做到了,因为当我删除它时,该方法会将空字节转换为图像,但是当我放置此方法时,它会等到它的 not = 0
  • @TarekAdel:什么方法可以将空字节转换为图像?什么是空字节?你把什么放在哪里,什么不等于0?您介意详细说明您的上一条评论吗?
  • @TarekAdel:当您收到您在对该答案的第一条评论中描述的错误时,客户转换它的大小有多大?
猜你喜欢
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-19
  • 2013-07-04
相关资源
最近更新 更多