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

Posted on 2005-04-13
Last Modified: 2008-01-09


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!
Question by:mantany
    LVL 11

    Expert Comment


    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);


    Author Comment

    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.
    LVL 2

    Expert Comment

    by:Mohammad Aamir Qureshi

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

    LVL 2

    Expert Comment

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


    Author Comment

    Hi, i want CODE

    I will be thankfull if you got some

    Author Comment

    And yes the picture should be view true picturebox
    LVL 2

    Accepted Solution

    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


    Author Comment


    Thanks man it works fine :)

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now