CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2010
    Posts
    29

    byte array to image

    Hello everyone,

    I'm having problem converting a byte array to an image. My program begins by converting an image to a byte array, then sends it across the Internet. The TCP server then listens for the incoming packets and SHOULD then convert the byte array back to an image.

    An invalid parameter error occurs when trying to convert the byte array to an image.

    Code:
            static void Connect(String server, byte[] Image)
            {
                try
                {
                    // Create a TcpClient.
                    Int32 port = 9050;
                    TcpClient client = new TcpClient(server, port);
    
                    NetworkStream stream = client.GetStream();
    
                    stream.Write(Image, 0, Image.Length);
                    Console.WriteLine("Sent image...");
    
                    stream.Close();
                    client.Close();
                }
                catch(ArgumentNullException e)
                {
                    Console.WriteLine("ArgumentNullException: {0}", e);
                }
                catch(SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
            }
    
    
            //Listen for incoming packets
            public void Listen()
            {
                TcpListener server=null;
                try
                {
                    Int32 port = 9050;
                    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
    
                    // TcpListener server = new TcpListener(port);
                    server = new TcpListener(localAddr, port);
    
                    // Start listening for client requests.
                    server.Start();
    
                    // Buffer for reading data
                    Byte[] bytes = new Byte[1024];
                    Image img;
    
                    // Enter the listening loop.
                    while(true)
                    {
                        TcpClient client = server.AcceptTcpClient();
                        Console.WriteLine("Connected!");
    
                        img = null;
    
                        // Get a stream object for reading and writing
                        NetworkStream stream = client.GetStream();
    
                        int i;
    
                        // Loop to receive all the data sent by the client.
                        while((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {                        
                            img = byteArrayToImage(bytes);
                            
                            //Display recieved image
                            pictureBox1.Image = img;
    
                            Console.WriteLine("Recieved image.");
                        }
    
                        // Shutdown and end connection
                        client.Close();
                    }
                }
                catch(SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
                finally
                {
                    // Stop listening for new clients.
                    server.Stop();
                }            
            }
    
            public byte[] imageToByteArray(System.Drawing.Image imageIn)
            {
                MemoryStream ms = new MemoryStream();
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                return ms.ToArray();
            }
    
            public Image byteArrayToImage(byte[] byteArrayIn)
            {
                MemoryStream ms = new MemoryStream(byteArrayIn);
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            }
    Any suggestions?

    Many thanks in advance,
    bassguru

  2. #2
    Join Date
    Feb 2010
    Posts
    29

    Re: byte array to image

    Perhaps it would help if I showed you where I believe the error is occuring.

    Code:
            static void Connect(String server, byte[] Image)
            {
                try
                {
                    // Create a TcpClient.
                    Int32 port = 9050;
                    TcpClient client = new TcpClient(server, port);
    
                    NetworkStream stream = client.GetStream();
    
                    stream.Write(Image, 0, Image.Length);
                    Console.WriteLine("Sent image...");
    
                    stream.Close();
                    client.Close();
                }
                catch(ArgumentNullException e)
                {
                    Console.WriteLine("ArgumentNullException: {0}", e);
                }
                catch(SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
            }
    
    
            //Listen for incoming packets
            public void Listen()
            {
                TcpListener server=null;
                try
                {
                    Int32 port = 9050;
                    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
    
                    // TcpListener server = new TcpListener(port);
                    server = new TcpListener(localAddr, port);
    
                    // Start listening for client requests.
                    server.Start();
    
                    // Buffer for reading data
                    Byte[] bytes = new Byte[1024];
                    Image img;
    
                    // Enter the listening loop.
                    while(true)
                    {
                        TcpClient client = server.AcceptTcpClient();
                        Console.WriteLine("Connected!");
    
                        img = null;
    
                        // Get a stream object for reading and writing
                        NetworkStream stream = client.GetStream();
    
                        int i;
    
                        // Loop to receive all the data sent by the client.
                        while((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {                        
                            img = byteArrayToImage(bytes);    //This is what I think is responsible for the error. How do I get the stream and convert the data packets to a byte array???
                            
                            //Display recieved image
                            pictureBox1.Image = img;
    
                            Console.WriteLine("Recieved image.");
                        }
    
                        // Shutdown and end connection
                        client.Close();
                    }
                }
                catch(SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
                finally
                {
                    // Stop listening for new clients.
                    server.Stop();
                }            
            }
    
            public byte[] imageToByteArray(System.Drawing.Image imageIn)
            {
                MemoryStream ms = new MemoryStream();
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                return ms.ToArray();
            }
    
            public Image byteArrayToImage(byte[] byteArrayIn)
            {
                MemoryStream ms = new MemoryStream(byteArrayIn);
                Image returnImage = Image.FromStream(ms);                // Error: Parameter is not valid!
                return returnImage;
            }
    I think the problem is that I am not correctly taking the incoming data packets and then converting them into a byte array (which can then be subsiquently converted to an image). Any suggestions on how to do this properly?

    Thanks,
    bassguru

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured