Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4028074
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:10:32+00:00 2026-05-20T11:10:32+00:00

I’m a C# beginner facing problems regarding sending an image from a client to

  • 0

I’m a C# beginner facing problems regarding sending an image from a client to a server. I use the following code :

Client :

try
{
    Bitmap desktopBMP = CaptureScreen.CaptureDesktop();
    Image image = (Image)desktopBMP;
    MemoryStream ms = new MemoryStream();
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

    if (m_clientSocket != null)
    {
       byte[] data = ms.ToArray();
        m_clientSocket.Send(data);
    }
}
catch (Exception e)
 {
       sending = false;
        MessageBox.Show(e.Message);
 } 

Server :

 // This the call back function which will be invoked when the socket
 // detects any client writing of data on the stream

 static int i = 0;
 public void OnDataReceived(IAsyncResult asyn)
 {  
     SocketPacket socketData = (SocketPacket)asyn.AsyncState;
     try
     {
          // Complete the BeginReceive() asynchronous call by EndReceive() method
          // which will return the number of characters written to the stream 
          // by the client
          int iRx = socketData.m_currentSocket.EndReceive(asyn);
          byte[] data = new byte[iRx];
          data = socketData.dataBuffer;
          MemoryStream ms = new MemoryStream(data);
          Image image = Image.FromStream(ms);
          image.Save(i + socketData.m_clientNumber+".jpg");
          i++;

      // Continue the waiting for data on the Socket
         WaitForData(socketData);

     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message);
     } 


// Start waiting for data from the client
   public void WaitForData(SocketPacket socketPacket)
   {
       try
       {
         if (pfnWorkerCallBack == null)
         {
             // Specify the call back function which is to be 
             // invoked when there is any write activity by the 
             // connected client
             pfnWorkerCallBack = new AsyncCallback(OnDataReceived);  
         }

         socketPacket.m_currentSocket.BeginReceive(socketPacket.dataBuffer, 
                                                   0,
                                                   socketPacket.dataBuffer.Length,
                                                   SocketFlags.None,
                                                   pfnWorkerCallBack,
                                                   socketPacket
                                                  );
       }
       catch (Exception e)
       {
           MessageBox.Show(e.Message);
       }
    }

The client sends images at every 100 ms and this works well for a while but sometimes a “ArgumentException” is thrown by the called “System.Drawing.Image.FromStream()” function in the server.

What am I doing something wrong here ? and how should I correct it ?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:10:33+00:00Added an answer on May 20, 2026 at 11:10 am

    Looking at the MSDN page for Image.FromStream, it states that the parameter is either null or contains an invalid image when that exception is thrown. Can you also send a hash of the image data to the server, which it could then use to validate your image data hasn’t been corrupted in transit? If it is corrupt the server could notify the client that it should resend the image. I suspect that you are running so much data that the odds are you are receiving an occasional corrupt bit.

    Also add a temporary check just to make sure your socketData.dataBuffer hasn’t somehow been set to null while you are debugging this issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.