Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image c图像发送改进_Image_Sockets_Screen_Share - Fatal编程技术网

Image c图像发送改进

Image c图像发送改进,image,sockets,screen,share,Image,Sockets,Screen,Share,我试着每秒发送尽可能多的图片,它在本地网络上运行得很好,但是当我在朋友的电脑上尝试时,或者在某些时候。。。它工作得很慢。。我有一个很好的网络,我的朋友也有。。我只打开了这个端口 我认为这可能与图像压缩或其他有关 这是客户的代码 private void startSend() { sck = client.Client; s = new NetworkStream(sck); while (true) { Bitmap screeny = n

我试着每秒发送尽可能多的图片,它在本地网络上运行得很好,但是当我在朋友的电脑上尝试时,或者在某些时候。。。它工作得很慢。。我有一个很好的网络,我的朋友也有。。我只打开了这个端口

我认为这可能与图像压缩或其他有关

这是客户的代码

 private void startSend()
 {
    sck = client.Client;
    s = new NetworkStream(sck);

    while (true)
    {
        Bitmap screeny = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
        Graphics theShot = Graphics.FromImage(screeny);
        theShot.ScaleTransform(.25F, .25F);
        theShot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
        BinaryFormatter bFormat = new BinaryFormatter();

       bFormat.Serialize(s, screeny);

       theShot.Dispose();
       screeny.Dispose();

       Thread.Sleep(20);
   }
}
服务器是:

    public void startListening()
    {
        listener = new TcpListener(IPAddress.Any, 10);
        listener.Start();

         stream= listener.AcceptTcpClient().GetStream();

         while (true)
         {
             BinaryFormatter bFormat = new BinaryFormatter();
             Bitmap inImage = bFormat.Deserialize(stream) as Bitmap;

             theImage.Image = (Image)inImage;
         }
    }
这两种方法都适用于线程。。。即使我试着把睡眠时间设为3000毫秒,速度也很慢


伙计们,你知道我怎样才能更快地发送吗?idk对压缩一无所知,所以如果有人能帮助大家D

您可以在发送之前压缩文件:

尝试将以下方法添加到客户端:

byte[] Compress(byte[] b)
{
    using (MemoryStream ms = new MemoryStream())
    {
            using (GZipStream z = new GZipStream(ms, CompressionMode.Compress, true))
            z.Write(b, 0, b.Length);
        return ms.ToArray();
    }
}

byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
将您的呼叫更改为:

byte[] compressed = Compress(ImageToByte(screeny));
bFormat.Serialize(s, compressed);
并将这些添加到服务器:

byte[] Decompress(byte[] b)
{
    using (var ms = new MemoryStream())
    {
        using (var bs = new MemoryStream(b))
        using (var z = new GZipStream(bs, CompressionMode.Decompress))
            z.CopyTo(ms);
        return ms.ToArray();
    }
}

public static Bitmap ByteToImage(byte[] imageData)
{
    using (var ms = new MemoryStream(imageData))
    {
        return new Bitmap(ms);
    }
}
并将服务器上的while块内部更改为:

BinaryFormatter bFormat = new BinaryFormatter();

byte[] inBytes = bFormat.Deserialize(stream) as byte[];
Bitmap inImage = ByteToImage(Decompress(inBytes ));

theImage.Image = (Image)inImage;

我真的不明白,兄弟。。之后如何向服务器发送zip?。。让我们假设我设法做到了这一点,我怎么能在另一边解压呢?isnt听起来太长太复杂,我是说在效率方面。如果你在本地运行,你的代码很快,在网络上运行需要时间,你的问题是网络流量。你们在客户机上压缩,在服务器上解压,并没有那个么复杂……我现在正在做一些事情,稍后我可以在你们的代码中发布如何做到这一点好吧,兄弟!我真的很想看看你的代码:D