Welcome to The Scripts.
When the error occurs, what method are you calling? What are the objects you are passing to the MemoryStream object? Could we see the code where the error is occuring?
Sending Code -- At client side
public void Send()
{
Image img = Image.FromFile(@"C:\image.jpg");
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
byte[] buf2 = ms.GetBuffer();
byte[] buf = new byte[(buf2.Length/2) + 1];
for (int i = 0; i < buf2.Length; i++)
{
if (i % 2 == 0)
{
buf[i / 2] = buf2[i];
}
}
udpClient.Send(buf, buf.Length,"localhost",8001);
ms.Close();
img.Dispose();
}
************************************************** ***********************
//Receiving Code -- At the server side
public void Receive()
{
UdpClient udpClient = new UdpClient(8001);
while (true)
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
byte[] buf2 = udpClient.Receive(ref endPoint);
byte[] buf = new byte[buf2.Length * 2];
for (int i = 0; i < buf.Length; i+=2)
{
buf[i] = buf2[i/2];
buf[i+1]=buf2[i/2];
}
//The ERROR occurs in the following line: Invalid parameter
MemoryStream ms = new MemoryStream(buf);
Image img = Image.FromStream(ms);
Invoke(new UpdatePicBoxDelegate(UpdatePicBox), new object[] { img });
ms.Close();
}
}