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

  • SEARCH
  • Home
  • 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 7735239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:27:04+00:00 2026-06-01T07:27:04+00:00

I have a file transfer program. The program (Client) does following operations to send

  • 0

I have a file transfer program. The program (Client) does following operations to send a bitmap via TCP socket: get screenshot -> grab Bitmap from memory -> convert to stream -> send

        MemoryStream Fs = new MemoryStream();

//////////////11111
        Bitmap bmp = TakeScreen();

///////////////2222
        //Bitmap bmp = new Bitmap(@"C:\temp\001.bmp");


        bmp.Save(Fs, ImageFormat.Bmp);
        Byte[] buffer = Fs.ToArray();

        Fs.Read(buffer, 0, buffer.Length);
        TcpClient socket = new TcpClient("192.168.0.131", 1095);

        NetworkStream nw = socket.GetStream();
        nw.Write(buffer, 0, buffer.Length);
        nw.Close();
        Fs.Dispose();
        socket.Close();
        bmp.Dispose();
  • If I choose to transfer image directly from memory – no errors.

  • If I try to load Bitmap from file first – getting “Parameter is not valid” error on a server’s side.

here is the server side:

                    NetworkStream Nw = new NetworkStream(handlerSocket.Client);
                    int thisRead = 0;
                    int Blocksize = 1024;
                    Byte[] dataByte = new Byte[Blocksize];

                    Bitmap screen = getScreen(Nw, dataByte, thisRead, Blocksize);
                    Nw.Close();

and

   private Bitmap getScreen(NetworkStream Nw, Byte[] dataByte, int thisRead, int Blocksize)
    {
        Bitmap bitmap;
        using (var strm = new MemoryStream())
        {

            while (true)
            {
                thisRead = Nw.Read(dataByte, 0, Blocksize);
                strm.Write(dataByte, 0, thisRead);
                if (thisRead == 0)
                    break;
            }

            bitmap = new Bitmap(strm); // Error here
        }
        Bitmap bm3 = new Bitmap(bitmap);
        bitmap.Dispose();
        return bm3;
    }

What’s causing this error? I guess it has something to do with the MemoryStream.

edit: simplified the question

  • 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-06-01T07:27:05+00:00Added an answer on June 1, 2026 at 7:27 am

    You nee to seek to the beginning of the stream before you can create the Bitmap in getScreen.

    private Bitmap getScreen(NetworkStream Nw, Byte[] dataByte, int thisRead, int Blocksize)
    {
        Bitmap bitmap;
        using (var strm = new MemoryStream())
        {
            while (true)
            {
                thisRead = Nw.Read(dataByte, 0, Blocksize);
                strm.Write(dataByte, 0, thisRead);
                if (thisRead == 0)
                   break;
            }
    
            stream.Seek(0, SeekOrigin.Begin; // <-- Go Back to beginning of stream
            bitmap = new Bitmap(strm); // Error here
        }
        Bitmap bm3 = new Bitmap(bitmap);
        bitmap.Dispose();
        return bm3;
    }
    

    EDIT
    Detailed explanation: After writing the last byte to the stream, the stream’s current position is a the end of the stream. Creating a Bitmap from the screen now tries to read the bitmap from the stream, which doesn’t work, as there is no more data after the current position (= the end of the stream).

    So what you need to do is tell the stream to set the current position back to the beginning of the stream. Then, the bitmap information can be read.

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

Sidebar

Related Questions

I have a simple file transfer socket program where one socket sends file data
I have a client and server, client sending file to server. When i transfer
I am working on a binary file transfer program in which the client has
I have created small socket programs (client/server). Client will transfer all formats of files
I have implemented a file transfer rate calculator to display kB/sec for an upload
I have to develop a file server service that transfer file form a specified
I'm trying to write a secure transfer file program using Python and AES and
I want to ask, why I cannot transfer file from server to client? When
I am making a protocol, client and server which provide file transfer functionality similar
I have moved my file transfer service from basicHttpBinding to netTcpBinding as I am

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.