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 < length)
     currentPosition += tt1.GetStream().Read(tempBytes, currentPosition, length - currentPosition);
     newImage = Image.FromStream(ms);

  currentPosition = 0;
  while (currentPosition < 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?


Pseudo code of what I mean:

Read length from Stream:

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.

  • \$\begingroup\$ Are you saying that the NetworkStream can contain multiple images? \$\endgroup\$ – svick Aug 19 '13 at 11:50
  • \$\begingroup\$ No, i am saying i am sending the length (first 4 byte) then the actual image. So i get the Image in one stream, so i want to know if i can just convert that to an image immediately, without using a memorystream as a "handler":) \$\endgroup\$ – Zerowalker Aug 19 '13 at 11:56
  • 1
    \$\begingroup\$ you should check out StackOverflow and see if your question fits better there. also check out Code Review's About \$\endgroup\$ – Malachi Aug 22 '13 at 15:44
  • \$\begingroup\$ I know, and well i do use it. But sadly, i have used my 30 questions per month. So i try to find limited questions that fits the Review term. \$\endgroup\$ – Zerowalker Aug 22 '13 at 16:20

my though is like this, though i didn't really test it

  1. 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;

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

  3. 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.


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.

| improve this answer | |
  • \$\begingroup\$ Well, it´s pretty much what i am doing currently. I split up 2 transfers, so i get length in 1 turn and the image in another. But why can´t i use Image.FromStream in one turn and tell it to read "This" much data? As you say, it waits till the end of the stream (Networkstream doesn´t have an end from what i can grasp), so i would have to close the connection for it to know that. Which isn´t optimal at all, which is why i have the length so i can know when it´s done. \$\endgroup\$ – Zerowalker Aug 19 '13 at 13:31
  • \$\begingroup\$ Not really an answer to the question, but as it´s off topic, this general explanation fits the place. Thanks \$\endgroup\$ – Zerowalker Aug 20 '13 at 4:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.