从服务器向客户端发送图片时出错

时间:2014-05-26 16:42:26

标签: c# sockets streaming tcpclient bitmapimage

我有2个应用程序(服务器 - 客户端)。 服务器是TVsharp的修改版本(使用RTLSDR传输本地模拟电视信号的应用程序) 流式视频的每个帧都是字节的灰度数组。 我已对其进行了修改,以便重新调整大小并通过TCP套接字为客户端发送每个帧

客户端应该通过套接字接收帧作为Image对象并将它们显示在图片框中 我得到无效的参数错误。 添加延迟(Thread.Sleep())后,它开始显示一帧,然后它给出无效的参数异常(在休眠时间之后)

这是TVsharp发送的一部分: 灰度是一个包含每个像素亮度的数组

   private string drive = "E:\\";
        private string file = "0";
        private string extension = ".bmp";
        private string path2 = "E:\\test\\";
        private string fullpath;
        private int file_counter = 0;



           Bitmap bitmap2 = new Bitmap(_pictureWidth, _pictureHeight);
            var data2 = bitmap2.LockBits(new Rectangle(Point.Empty, bitmap2.Size),
           ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb);
           Marshal.Copy(GrayScaleValues, 0, data2.Scan0, GrayScaleValues.Length);
           bitmap2.UnlockBits(data2);
           bitmap2.Save(fullpath, System.Drawing.Imaging.ImageFormat.Bmp);

                string Npath = path2 + file + extension; 

                Image img = Image.FromFile(fullpath);
               // Size size = new Size(982, 543);
               // ResizeImage(img, size);

                Rectangle cropRect = new Rectangle(80, 0, 240, 175);
                Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
                using (Graphics g = Graphics.FromImage(target))
                {
                    g.DrawImage(img, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel);
                }
                //double scale = 203 / 96;
                int width = (int)(target.Width);
                int height = (int)(target.Height);

             BinaryWriter brn = new BinaryWriter(s);
                System.Drawing.Bitmap bmpScaled = new System.Drawing.Bitmap(target, width, height);

               bmpScaled.Save(Npath);




                   byte[] imageArray = File.ReadAllBytes(Npath);

                   sw.WriteLine(imageArray.Length.ToString());
                   sw.Flush();
                  // Thread.Sleep(500);
                   brn.Write(imageArray);
                   //Thread.Sleep(500);

    _detectLevel = Convert.ToInt32(_maxSignalLevel * _detectorLevelCoef);
    _agcSignalLevel = agcMaxLevel;
}

此客户端是假定获取帧并显示它们的客户端段

        flag = true;

        TcpClient client = new TcpClient(textBox1.Text, Convert.ToInt32(textBox2.Text));
        Stream s = client.GetStream();

        StreamReader sr = new StreamReader(s);
        StreamWriter sw = new StreamWriter(s);
        sw.Flush();


        BinaryFormatter formatter = new BinaryFormatter();
        string msg = sr.ReadLine();
        MessageBox.Show("It's working !! the message is " + msg);

        BinaryReader Brn = new BinaryReader(s);


        Thread.Sleep(5000);
        while (flag)
        {
           // Thread.Sleep(5000);
            int size = Convert.ToInt32(sr.ReadLine());


            label3.Text = "Size is " + size;




            byte[] imagerray = Brn.ReadBytes(size);



            MemoryStream ms = new MemoryStream(imagerray);
            Thread.Sleep(10000);
            Image image = Image.FromStream(ms);

            ResizeImage(image, this.Size);
            pictureBox1.Image = image;
            Thread.Sleep(10);

        }
    }

0 个答案:

没有答案