简体   繁体   中英

Parameter is not valid - error when sending a bitmap via TCP with C#

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM