将图像从客户端发送到服务器

时间:2017-03-02 14:15:28

标签: c# .net sockets tcp

所以,我试图在服务器和客户端之间发送图像,但是,当服务器收到图像时,图像不会呈现所有图像。

客户端代码:

//button click
Bitmap tImage = new Bitmap(@"C:\Users\Milan\Downloads\guitarstill.gif");

            byte[] bStream;
            //if (string.IsNullOrEmpty(tbPayload.Text)) return;
            try
            {
                bStream = imageToByteArray(tImage);
                if (mTcpClient != null)
                {
                    if (mTcpClient.Client.Connected)
                    {
                        mTcpClient.GetStream().BeginWrite(imageToByteArray(tImage), 0, imageToByteArray(tImage).Length, onCompleteWriteToServer, mTcpClient);
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }


//Within another method:
public byte[] imageToByteArray(System.Drawing.Image imageIn) {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            //richTextBox1.Text = ms.ToArray().ToString();
            return ms.ToArray();
}

服务器端代码:

void onCompleteReadFromTCPClientStream(IAsyncResult iar)
 {
TcpClient tcpc;
        int nCountReadBytes = 0;
        string strRecv;
        ClientNode cn = null;

        try
        {
            lock (mlClientSocks)
            {
                tcpc = (TcpClient)iar.AsyncState;

                cn = mlClientSocks.Find(x => x.strId == tcpc.Client.RemoteEndPoint.ToString());

                nCountReadBytes = tcpc.GetStream().EndRead(iar);

                if (nCountReadBytes == 0)// this happens when the client is disconnected
                {
                    MessageBox.Show("Client disconnected.");
                    mlClientSocks.Remove(cn);
                    lbClients.Items.Remove(cn.ToString());
                    return;
                }

                strRecv = Encoding.ASCII.GetString(cn.Rx, 0, nCountReadBytes);
                //strRecv = Encoding.ASCII.GetString(mRx, 0, nCountReadBytes);

                if (strRecv.StartsWith("GIF"))
                {
                    //MemoryStream ms = new MemoryStream(cn.Rx);

                    //Image x = (Bitmap)((new ImageConverter()).ConvertFrom(cn.Rx));


                    pictureBox1.Image = byteArrayToImage(cn.Rx);

                    /*
                    lock (mlClientSocks)
                    {

                        //if (cn != null && cn.tclient != null && cn.tclient.Client.Connected)
                        //{
                        //foreach (var clients in spectatorsIPAndPort)
                        //{
                        cn = mlClientSocks.Find(x => x.strId == clients);
                        cn.Tx = new byte[512];

                        cn.Tx = Encoding.ASCII.GetBytes(strRecv);
                        cn.tclient.GetStream().BeginWrite(cn.Tx, 0, cn.Tx.Length, onCompleteWriteToClientStream, cn.tclient);
                        //Console.WriteLine("Sent Number of Clients via Request: " + Encoding.UTF8.GetString(cn.Tx) + " To " + clients);
                        //}
                        //}

                    }
                    */



                    //printLine(DateTime.Now + " - " + cn.ToString() + ": " + strRecv);


                }

                cn.Rx = new byte[512];

                tcpc.GetStream().BeginRead(cn.Rx, 0, cn.Rx.Length, onCompleteReadFromTCPClientStream, tcpc);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            lock (mlClientSocks)
            {
                printLine("Client disconnected: " + cn.ToString());
                mlClientSocks.Remove(cn);
                lbClients.Items.Remove(cn.ToString());
            }

        }
    }


//within another method
public Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn, 0, byteArrayIn.Length);
            //ms.Position = 0;
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

我的输出:

enter image description here

预期输出: - 此完整图像

enter image description here

0 个答案:

没有答案