Get Image From Stream, TCP Stream without converting to MemoryStream first IS it possible to read directly from the TCPStream to get an image if I know the size and all that, without first saving it to a memorystream?

My code currently works with putting the first 4 byte in the TCP stream as a length for the entire data, so first I send the length. And then I know the length so I recieve it.

But I am saving it to a MemoryStream first, and it would save time to get it directly from the Stream if possible.

  tempBytes = new byte[length];
  ms = new MemoryStream(tempBytes);

     int currentPosition = 0;
     while (currentPosition &lt; length)
     {
     currentPosition += tt1.GetStream().Read(tempBytes, currentPosition, length - currentPosition);
     }
     newImage = Image.FromStream(ms);

  currentPosition = 0;
  while (currentPosition &lt; intsize)
   {
    currentPosition += tt1.GetStream().Read(lenArray, currentPosition, intsize - currentPosition);
   }

    length = BitConverter.ToInt32(lenArray, 0);


So this is in a while loop, so you can see, I get the length (well I get it at the end, but I have already got it before the while loop for the first run). Then I read the length of data into a memorystream, then get the image.

But as a NetworkStream is a Stream already, there should be possible to just read it directly.

So is there a way to do this to improve the performance?

EDIT:

Pseudo code of what I mean:

Read length from Stream:

tt1.GetStream().Read(lenArray,0,4);
length = BitConverter.ToInt32(lenArray, 0);


Read and get the Image

Image = tt1.GetStream().Read(data,0,length)


This of course doesn´t work, but it´s what I am trying to do.
I want to read directly from the stream, and as I know the length and all that, it should be possible I think, without having to save to a byte array inside a memorystream.
my though is like this, though i didn't really test it


You must keep the stream open for the lifetime of the Image.  because the Image.FromStream  wont read the data until it needs it, for performance purpose;
you must make sure the all the data for that image is received before you call Image.FromStream. TCP could split your data into multiple packages when transfer.  since you transfer 4 bytes as the length of that image, you can read the length first, then you can keep checking the length of that NetworkStream, but do not read any data.  or you can peek the data but make sure the data is still in the stream;
the last condition, do not transfer more data after the image until you are done with the image. otherwise, the data after the image could be treated as the image data.  if it is hard for your current application's protocol, you might just use a separated tcp connection for image data only.


Update:

Or I think you can derived a class from System.Stream and override the Read method and some other methods as well.  Now you can manage how you read the data.  This way, you can even work on just the same TCp connection because you know the length.   Just pass that derived Stream to the Image.FromStream, that should/might work.