[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1336
  • Last Modified:

how to send a image from a client to server true socket?


im new to c-sharp and just a hobby programmer, i found samples on internet for send and recive text msg true socket (most of them not good) but i didn't find any for sending image.

I need to know how to get a image from file on local disk and sent to the server true a client.  Both server and client are on same computer. (localhost)

And is it possible to specify to a folder with more pictures and ask for picture update? So i can see the pictures change.
And the possibillity to choose between 3 folders with pictures.

I will be thankfull if anybody can give me a complete solution for this in c#

Thank you so much in advance to all of you!
  • 4
  • 3
1 Solution

1. Read image in memory stream using encoder.
2. Send this memorystream( which will be byte stream ) to the other end (client).
3. And in client just capture this memory stream use the decoder and display the image in Picture box.

Use this method for saving Image to any stream in required format
here stream in ur case will be memory stream.

This can be done only if u r ready to use the TcpStreams.
If u really are intrested in using Sockets. Have a look on this article

public static void Save(Image BM, Stream S,
            ImageFormat Format, int Quality)

// Save the Bitmap to the Stream. If it's
// in JPEG format, save with the specified
// Quality level.
  if (Format != ImageFormat.Jpeg)
    // Save non-JPEG images without
    // adjusting the Quality level
    BM.Save(S, Format);
    // Adjust quality level of JPEG images.

    // Create an EncoderParameters object
    // containing the Quality level as a
    // parameter.
    EncoderParameters Params =
      new EncoderParameters(1);
    Params.Param[0] =
      new EncoderParameter(Encoder.Quality, Quality);

    // Save the image using the JPEG encoder
    // with the specified Quality level.
    BM.Save(S, GetEncoderInfo("image/jpeg"), Params);

mantanyAuthor Commented:
i have never used socket before, so i need to know more specific how to program the client socket to find image (.jpg) files from a specified folder(maybe i have to put the images in the project folder, i have no idea about whats the easiets way to get the images) on my local disc and send it to the server socket. And how to program the server socket to recive the image file.

I know that i have to send it byte by byte. But i have no idea how and how to integrate it into the socket program.
Mohammad Aamir QureshiCommented:

u want CODE or Suggestion to send a picture from client to server and in server u view picture in picture box ??????

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Mohammad Aamir QureshiCommented:
What u want CODE with explanation or simple suggestion

mantanyAuthor Commented:
Hi, i want CODE

I will be thankfull if you got some
mantanyAuthor Commented:
And yes the picture should be view true picturebox
Mohammad Aamir QureshiCommented:
First check this code and then tell me if u face some problem
/// make two Window Application separetely one for Client and Second for Server

// Code For Client Side

using System.Net.Socket  // For Socket Programming
using System.IO            // For Read and Write File
    Stream os = File.OpenWrite(@"C:\test\test2.jpg");
    byte [] buffer = new byte[87619];
    TcpClient sServer = new TcpClient("localHost", 65000);
     NetworkStream networkStream = sServer.GetStream();
    int no = networkStream.Read(buffer,0,87619);
Catch(Exception Ex)

//// Code For Server Side

TcpListener tcpListener = new TcpListener(65000);
int no;
for (;;)
Socket socket = tcpListener.AcceptSocket();
if (socket.Connected)
   NetworkStream networkStream = new NetworkStream(socket);
   Stream os = File.OpenRead(@"C:\test\test.jpg");
   byte[] buffer = new byte[os.Length];

Simply Write Down this Code and create the folder with the name of test and inside the test folder put the .JPG picture with the name of test.jpg
then u will see the server send the picture to client and if u want to send the picture from client to server then change the code

mantanyAuthor Commented:

Thanks man it works fine :)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now