I am trying to receive the image form Server and want to display it into a Picturebox in WM Application. I am successfully receiving the Image Stream and I don’t find any way to display it into a PictureBox. In windows program we have a method in Image class that is FromStream (Image.FromStream) but this function is not available in Compact Framework 3.5. I also tried the following code to do so:
private void button1_Click(object sender, EventArgs e)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress IP = IPAddress.Parse("192.168.1.2");
IPEndPoint IPE = new IPEndPoint(IP, 4321);
s.Connect(IPE);
byte[] buffer = new byte[55296];
s.Receive(buffer, buffer.Length, SocketFlags.None);
MemoryStream ms = new MemoryStream(buffer);
Image im = new Bitmap(ms); //EXCEPTION
pictureBox1.Image = im;
}
But it gives an Exception. No detail is provided with the exception and VS is only displaying a dialogbox with the text “Exception”.
Does your image size is less than the size of the buffer? If not all the excess data is lost and an exception is thrown.
Also could you try without the buffer length specified.