Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to send an image from the server to the client. the server sent the image but the client doesn't display it ..I have a problem with the code.
can anyone help me, please.

What I have tried:

server\\
private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                Image img = Image.FromFile("D:\\1.jpg");
                MemoryStream ms = new MemoryStream();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                bytes = ms.ToArray();
                acc.Send(bytes, 0, bytes.Length, 0);
                MessageBox.Show("ok");
            }
            catch
            {
                MessageBox.Show("Failure....");
            }
            
        }

client\\
try
            {
                sock.Connect(new IPEndPoint(IPAddress.Parse(textBox3.Text), 3));
                
                new Thread(() =>
                {
                    read();
                }).Start();


            }
            catch
            {
                MessageBox.Show("failure");
            }


        }

        void read()
        {
            while (true)
            {
                try
                {

                    byte[] buffer = new byte[255];
                    int rec = sock.Receive(buffer, 0, buffer.Length, 0);
                    if (rec <= 0)
                    {
                        throw new SocketException();
                    }
                    Array.Resize(ref buffer, rec);
                    Invoke((MethodInvoker)delegate
                    {
                        
                        MemoryStream imgstream = new MemoryStream(buffer);
                        Image imgfromstream = Image.FromStream(imgstream);
                        pictureBox1.Image = imgfromstream;


                    });

                }
                catch
                {
                    MessageBox.Show("Disconcting");
                    sock.Close();
                    break;
                }

            }
            Application.Exit();
        }
Posted
Updated 12-Mar-19 14:38pm
Comments
Thomas Daniels 12-Mar-19 18:02pm    
I haven't ran the code, but what is bytes.Length on the server before you send the image? Your buffer on the receiving end has only 255 bytes, it would not surprise me if your image is bigger than that.
Member 14129828 14-Mar-19 6:34am    
thank you for your answer ...It is exactly my problem. my buffer was small. I fixed it.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900