Transfer picture (jpg) over the network using NetworkStream

Normally people will think if they want to transfer the picture over network in between 2 computers then Socket.Send will be used. People think of that could be because of the confusion of the name of the namespace using System.Net.Sockets. They are right perhaps but I found it is easier to use NetworkStream.

In order to use NetworkStream include the namespace System.Net.Sockets (same namespace if you would like to use Socket).

using System.Net.Sockets;

The code divided into 2 parts, sending and receiving.

At the receiving side the code need to initial listening.

TcpListener server = null;

// Set the TcpListener on port 13000.

Int32 port = 13000;

IPAddress localAddr = IPAddress.Parse(“”);


// TcpListener server = new TcpListener(port);

server = newTcpListener(localAddr, port);


// Start listening for client requests.


The program will pulse at server.Start(); to wait for incoming packet. Set the buffer size to be ready to accept the incoming packet, the size can be bigger but not smaller.

// Buffer for reading data

Byte[] bytes = newByte[277577];

String data;

data = null; 


// Perform a blocking call to accept requests.

// You could also user server.AcceptSocket() here.

TcpClient client = server.AcceptTcpClient();


Read the stream and write to a file.

NetworkStream stream = client.GetStream();


int i;

i = stream.Read(bytes, 0, 277577);


BinaryWriter writer = newBinaryWriter(File.Open(“GoodLuckToMe.jpg”, FileMode.Create));





At the sending side, setup the channel, collect the bytes and stream over to the server.

byte[] photo;

publicvoid sockSendRec()


  Int32 port = 13000;

  TcpClient client = newTcpClient(“”, port);


  // Translate the passed message into ASCII and store it as a Byte array.

  Byte[] data = GetPhoto(“Surface.jpg”);        


  NetworkStream stream = client.GetStream();


  // Send the message to the connected TcpServer.

  stream.Write(data, 0, data.Length);


publicbyte[] GetPhoto(string filePath)


      FileStream stream = newFileStream(

          filePath, FileMode.Open, FileAccess.Read);


      BinaryReader reader = newBinaryReader(stream);

      BinaryWriter writer = newBinaryWriter(File.Open(“GoodLuckToMe.jpg”, FileMode.Create));


      photo = reader.ReadBytes((int)stream.Length);




      pictureBox1.ImageLocation = “GoodLuckToMe.jpg”;


      long ll = photo.Length;





      return photo;


Download the complete code from The sample file name is WinSockTransfer.rar. My MSN ID is

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress ( till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net and tagged , , . Bookmark the permalink.

3 Responses to Transfer picture (jpg) over the network using NetworkStream

  1. Henk says:


    Cant find or download the source code?
    Can you mail my the code?
    Thanks and Regards Henk

  2. says:

    You got a very fantastic website, Sword lily I observed it through yahoo.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s