I'm declaring a Byte[] array whose size varies according to the size of the picture frame that i receive from a Socket connection. Now whenever I run the program, i get an Overflow Exception at the point where I declare the Byte[] array and set its size. Step by step debugging shows that the variable "size" , whose value i get from the server side program and which will eventually be equal to the size of the array, never goes above a certain limit. I even declare it equal to zero at the end of the loop just to keep things safe. However, when I run the program, the size of the long integer "size" is HUGE....and obviously, Byte[] array that size cannot be declared, so program throws an Exception. Why does the long integer end up with such a high value even thought i reset it at the end of the loop and debugging shows it hardly goes above a few hundred thousand Here's the code:
Byte
[] countbytes = new Byte[ 8];s.Read(countbytes, 0, countbytes.Length);
long size = BitConverter.ToInt64(countbytes, 0);
if (s.DataAvailable == true)
{
Byte[] pic = new Byte[size]; // EXCEPTION THROWN HERE.
s.Read(pic, 0, pic.Length);
MemoryStream ms = new MemoryStream(pic);
Image finalImage = Image.FromStream(ms);
pictureBox1.Image = finalImage;
size = 0;
}