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;

Re: Visual C# General OverflowException was Unhandled

Thomas Li

First thing:

Could you give examples of your values (size, countbytes, ..)


Does BitConverter convert your data correctly Maybe it expects another format (this corresponds to number 1).


Could you set size to a value .. say .. 1MB manually and then run your code Does the rest work then


You're talking about a loop - where

Re: Visual C# General OverflowException was Unhandled


1. Ok, how's this for an example.... 213685496423654 (And i'm not kidding, thats what i end up with when i get the exception)

2. BitConverter converts it correctly, I've successfully displayed a single frame.

3. There's no point in setting size manually, I HAVE to know what the size of the image is in order to declare a Byte[] array so that the data that I receive is only the picture data and nothing else. By the way, I have noticed that size value doesnt change, as in, each frame's size is around 372546 bytes. Erm, I'm not exactly sure how to synchronize the sent and receive+display operations so I just inserted a timer with a 100ms delay on the client side and on the server side, i simply used a Thread.Sleep() method to stop sending data for 100ms..the ultimate goal is to display a video.

4. My mistake, it's not a loop, I was talking about an "if" condition. It's right at the end of the code that I pasted.

Re: Visual C# General OverflowException was Unhandled

Thomas Li

Ok ... 200 TeraBytes is quite much data

Can you give an example value for countBytes as well

ad 3) I know that's not what you need, but you could try it to see if it would work in case the main error lies in the value of size.

What type of Stream is s FileStream NetworkStream

To be honest I still think that BitConverter doesn't convert the data properly