How to change from Byte[] to Bitmap on a Pocket PC
-
Okay, the situation is this: I saved a Bitmap using the Bitmap.Save method into a MemoryStream and then I used the MemoryStream.ToArray() method to turn it into a Byte[] array. I used a Socket to send the the Byte[] (buffer) and recieved the buffer in a remote side (Mostly on a Pocket PC), I cannot seem to find a way to return that Byte[] back to a Bitmap, I tried using the MemoryStream(Byte[]) constructor in order to save it on a memory stream, and then using the Bitmap(System.IO.Stream) constructor but it returns an exception stating that it is an invalid parameter or argument, I would thank all who could help me solve this problem, here's an example of what I have been doing to send and recieve my Bitmap: //Sending: Bitmap bmp = new Bitmap(@openDialog1.FileName); System.IO.MemoryStream memImage = new MemoryStream(); bmp.Save(memImage,System.Drawing.Imaging.ImageFormat.Jpeg); byte[] data = memImage.ToArray(); IPEndPoint endPoint = new IPEndPoint(this.theIP,651); //this.theIP = my IP socket.Connect(endPoint); socket.Send(data,0,data.Length,SocketFlags.None); //Recieving IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("..my Ip.."),651); this.socket.Bind(endPoint); this.socket.Listen(10); Socket connected = socket.Accept(); byte[] byteResponse = new byte[1024]; connected.Receive(byteResponse,SocketFlags.None); MemoryStream memory = new MemoryStream(byteResponse); Bitmap bmp = new Bitmap(memory); //generates invalid argument exception this.pictureBox1.Image = bmp; Could anyone help me and also point out my errors here, I would be a great help, thanks in advanced.:) P.S. I really would prefer a method that would work in the Compact Framework, since I am working with a Pocket PC as a reciever.:)
-
Okay, the situation is this: I saved a Bitmap using the Bitmap.Save method into a MemoryStream and then I used the MemoryStream.ToArray() method to turn it into a Byte[] array. I used a Socket to send the the Byte[] (buffer) and recieved the buffer in a remote side (Mostly on a Pocket PC), I cannot seem to find a way to return that Byte[] back to a Bitmap, I tried using the MemoryStream(Byte[]) constructor in order to save it on a memory stream, and then using the Bitmap(System.IO.Stream) constructor but it returns an exception stating that it is an invalid parameter or argument, I would thank all who could help me solve this problem, here's an example of what I have been doing to send and recieve my Bitmap: //Sending: Bitmap bmp = new Bitmap(@openDialog1.FileName); System.IO.MemoryStream memImage = new MemoryStream(); bmp.Save(memImage,System.Drawing.Imaging.ImageFormat.Jpeg); byte[] data = memImage.ToArray(); IPEndPoint endPoint = new IPEndPoint(this.theIP,651); //this.theIP = my IP socket.Connect(endPoint); socket.Send(data,0,data.Length,SocketFlags.None); //Recieving IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("..my Ip.."),651); this.socket.Bind(endPoint); this.socket.Listen(10); Socket connected = socket.Accept(); byte[] byteResponse = new byte[1024]; connected.Receive(byteResponse,SocketFlags.None); MemoryStream memory = new MemoryStream(byteResponse); Bitmap bmp = new Bitmap(memory); //generates invalid argument exception this.pictureBox1.Image = bmp; Could anyone help me and also point out my errors here, I would be a great help, thanks in advanced.:) P.S. I really would prefer a method that would work in the Compact Framework, since I am working with a Pocket PC as a reciever.:)
Hi Gakujin, To construct an image object (in your case a Bitmap, which is derived from Image) from a MemoryStream object use the
Image.FromStream
static method. so simply replace:Bitmap bmp = new Bitmap(memory);
withImage img = Image.FromStream(memory);
I am not sure about the compact framework, but I presume loading from a stream should work, since you can save to a stream already. EDIT: Sorry I didn't notice that the Bitmap constructor already does this, so ignore me :) -
Hi Gakujin, To construct an image object (in your case a Bitmap, which is derived from Image) from a MemoryStream object use the
Image.FromStream
static method. so simply replace:Bitmap bmp = new Bitmap(memory);
withImage img = Image.FromStream(memory);
I am not sure about the compact framework, but I presume loading from a stream should work, since you can save to a stream already. EDIT: Sorry I didn't notice that the Bitmap constructor already does this, so ignore me :)Yes, you would have to use the
Bitmap
constructor sinceImage.FromStream
is not supported on .NET CF. Just FYI :)Microsoft MVP, Visual C# My Articles