Tarek Adel
Tarek Adel

Reputation: 161

Send and receive an image with a tcp socket

I've made these codes to send and receive an Image with a TCP socket but the receive code didn't work, hope you can help me this is the Send Code

public void SendImage()
{
    int ScreenWidth = Screen.GetBounds(new Point(0, 0)).Width;
    int ScreenHeight = Screen.GetBounds(new Point(0, 0)).Height;
    Bitmap bmpScreenShot = new Bitmap(ScreenWidth, ScreenHeight);

    Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
    gfx.CopyFromScreen(0, 0, 0, 0, new Size(ScreenWidth, ScreenHeight));
    bmpScreenShot.Save(Application.StartupPath + "/ScreenShot.jpg", ImageFormat.Jpeg);
    byte[] image = new byte[1];
    bmpScreenShot = ResizeBitmap(bmpScreenShot, 300, 300);

    image = ImageToByte(bmpScreenShot);
    //get the length of image (length of bytes)
    int NumberOfBytes = image.Length;
    //put the size into a byte array
    byte[] numberofbytesArray = BitConverter.GetBytes(NumberOfBytes);

    //send the size to the Client
    int sizesend = sck.Send(numberofbytesArray, 0, numberofbytesArray.Length, 0);
    if (sizesend > 0)
    {
        MessageBox.Show("Size Sent");
    }
    //send the image to the Client
    int imagesend =sck.Send(image, 0, NumberOfBytes, 0);
    if (imagesend > 0)
    {
        MessageBox.Show("Image Sent");
    }
}

And Here is the Receive Code

public void ReceiveImage()
{
    if (sck.Connected)
    {
        NetworkStream stream = new NetworkStream(sck);
        byte[] data = new byte[4];

        //Read The Size
        stream.Read(data, 0, data.Length);
        int size = IPAdress.HostToNetworkOrder(BitConverter.ToInt32(data,0));
        // prepare buffer
        data = new byte[size];

        //Load Image
        int read = 0;
        while (read != data.Length)
        {
           read += stream.Read(data, read, data.Length - read);
        }
        //stream.Read(data, 0, data.Length);
        //Convert Image Data To Image
        MemoryStream imagestream = new MemoryStream(data);
        Bitmap bmp = new Bitmap(imagestream);
        pictureBox1.Image = bmp;                    
    }
}

Edit After removing IPAdress.HostToNetworkOrder to the following

int size = BitConverter.ToInt32(data,0);

there's still a problem. The problem is when I send the size, it's sent as 5kb but when I receive it I find it to be closer to 2GB.

Further more, I get an error at this line

read += stream.Read(data, read, data.Length - read);

With the following message

Unable to read data from the transport connection. An operation on a socket could be performed because the system lacked sufficient buffer space or because a queue was full.

Upvotes: 0

Views: 4819

Answers (1)

Patrick
Patrick

Reputation: 17973

Don't use HostToNetwork order unless you are creating a server that should be compliant with a java server/client for instance. If you do, you also need to change the order of the int data buffer you are sending.

You may also benefit from writing the bytes you receive on the client directly in to the memorystream instead of allocating data and writing the bytes to that. And an important note, don't forget to set imagestream.Position = 0 before you hand it over to the bitmap constructor.

Upvotes: 1

Related Questions