Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to change from Byte[] to Bitmap on a Pocket PC

How to change from Byte[] to Bitmap on a Pocket PC

Scheduled Pinned Locked Moved C#
graphicshelptutorialdata-structuresperformance
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gakujin
    wrote on last edited by
    #1

    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.:)

    B 1 Reply Last reply
    0
    • G Gakujin

      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.:)

      B Offline
      B Offline
      Baris Kurtlutepe
      wrote on last edited by
      #2

      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); with Image 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 :)

      H 1 Reply Last reply
      0
      • B Baris Kurtlutepe

        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); with Image 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 :)

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        Yes, you would have to use the Bitmap constructor since Image.FromStream is not supported on .NET CF. Just FYI :)

        Microsoft MVP, Visual C# My Articles

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups