程序只能通过互联网发送缩放图像

时间:2014-08-19 17:08:08

标签: c# image tcp ip bytearray

我编写了一个tcp / ip-chat程序,它具有网络摄像头聊天和screentransfer等功能。

但问题是,它只能接收尺寸缩放的图片......

我将发布我的测试类的完整代码:

    //static Byte[] Ip = {
    //                 192,
    //                 168,
    //                 0,
    //                 215q                                                                                     
    //             };
    //static IPAddress IpAdress = new IPAddress(Ip);
    TcpListener sTCP = new TcpListener(IPAddress.Any, 8000);
    TcpClient cTCP;
    Byte[] ImageData;
    String IP = null;
    Int32 Port = 0;

    private void close(object sender, FormClosingEventArgs e)
    {
        sTCP.Stop();
        Environment.Exit(Environment.ExitCode);
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        Thread listthread = new Thread(new ThreadStart(StartListening));
        listthread.Start();
        Thread startReceiving = new Thread(new ThreadStart(Listen));
        startReceiving.Start();
    }

    void StartListening()
    {
        sTCP.Start();
        //9123
    }

    void startsendwebcam(Bitmap img)
    { 
        byte[] s;
        byte[] buffer;
        cTCP = new TcpClient(IP, Port);
        BinaryWriter sWriter = new BinaryWriter(cTCP.GetStream());

        buffer = Image2ByteArray(img, System.Drawing.Imaging.ImageFormat.Png);
        int cLength = buffer.Length;
        s = new byte[cLength];
        s = buffer;

        sWriter.Write(cLength);
        sWriter.Write(s);
        sWriter.Flush();
        sWriter.Close();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if (timer1.Enabled)
        {
            button3.Text = "Begin";
            textBox1.Enabled = true;
            timer1.Enabled = false;
        }
        else
        {   
            String[] split = textBox1.Text.Split(':');
            IP = split[0];
            Port = Convert.ToInt32(split[1]);
            button3.Text = "End";
            textBox1.Enabled = false;
            timer1.Enabled = true;
        }
    }
    private Bitmap CreateScreenshot(int left, int top, int width, int height)
    {
        Bitmap bmp = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(bmp);
        g.CopyFromScreen(left, top, 0, 0, new Size(width, height));
        g.Dispose();
        return bmp;
    }

    Bitmap ResizeImage(Bitmap imgToResize, Size size)
    {
            Bitmap b = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage((Image)b))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(imgToResize, 0, 0, size.Width, size.Height);
            }
            return b;
    }

    void Listen()
    {
        do
        {
            try
            {
                if (sTCP.Pending())
                {
                    cTCP = sTCP.AcceptTcpClient();
                    BinaryReader r = new BinaryReader(cTCP.GetStream());
                    ImageData = new Byte[r.ReadInt32()];
                    ImageData = r.ReadBytes(ImageData.Length);
                    pictureBox1.Image = ByteArray2Image(ImageData);
                    r.Close();
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        while (true);
    }

    public byte[] Image2ByteArray(Image Bild, System.Drawing.Imaging.ImageFormat Bildformat)
    {
        System.IO.MemoryStream MS = new System.IO.MemoryStream();
        Bild.Save(MS, Bildformat);
        MS.Flush();
        return MS.ToArray();
    }

    public Image ByteArray2Image(byte[] ByAr)
    {
        Image img = default(Image);
        System.IO.MemoryStream MS = new System.IO.MemoryStream(ByAr);
        return Image.FromStream(MS);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        int quality = trackBar1.Value * 10;
        if (quality != 0)
        {
            startsendwebcam(ResizeImage(CreateScreenshot(0, 0, 800, 800), new Size(quality, quality)));
        }
    }
}

我可以通过互联网发送和接收图像;我做过portforwarding; 但它只能接收缩放图像。 是否有可能发送和接收高清图片?

目前的结果是; http://www.directupload.net/file/d/3719/9sel75ej_png.htm

1 个答案:

答案 0 :(得分:0)

耶! 我懂了!

它现在可以发送尺寸超过800的照片!

服务器仍有bug,如果有人对TCPImageSending感兴趣,我可以修复服务器......

http://ageofadventure.bplaced.net/WORKING.png

项目: http://ageofadventure.bplaced.net/TCPIPImageClass.zip

玩得开心!