Partage
  • Partager sur Facebook
  • Partager sur Twitter

network streaming c#

    26 décembre 2014 à 15:21:38

    hey all, i have a project that consists to send video stream from a raspberry pi to any other windows form application . the client side ( developed in python on the raspberry is working)

    import io import socket import struct import time import picamera

    Connect a client socket to my_server:8000 (change my_server to the

    hostname of your server)

    client_socket = socket.socket() client_socket.connect(('my_server', 8000))

    Make a file-like object out of the connection

    connection = client_socket.makefile('wb') try:

    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        # Start a preview and let the camera warm up for 2 seconds
        camera.start_preview()
        time.sleep(2)
    
        # Note the start time and construct a stream to hold image data
        # temporarily (we could write it directly to connection but in this
        # case we want to find out the size of each capture first to keep
        # our protocol simple)
        start = time.time()
        stream = io.BytesIO()
        for foo in camera.capture_continuous(stream, 'jpeg'):
            # Write the length of the capture to the stream and flush to
            # ensure it actually gets sent
            connection.write(struct.pack('<L', stream.tell()))
            connection.flush()
            # Rewind the stream and send the image data over the wire
            stream.seek(0)
            connection.write(stream.read())
            # If we've been capturing for more than 30 seconds, quit
            if time.time() - start > 30:
                break
            # Reset the stream for the next capture
            stream.seek(0)
            stream.truncate()
    # Write a length of zero to the stream to signal we're done
    connection.write(struct.pack('<L', 0))
    

    finally:

    connection.close()
    client_socket.close()
    

    in the server side on my desktop, when i run the project on visual studio , it's connected and receives the stream but the form is freezed and not responding and no image is displayed in the picturebox but when i stop the program , the image is saved on my desktop. i want to see the image in the picturebox .

    i will be so thankful if you can help me

    this is the code in c# :

    public partial class Form1 : Form

    {
        static string output = "";
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
    
        }
        public void createListener()
        {
            // Create an instance of the TcpListener class.
            TcpListener tcpListener = null;
            IPAddress ipAddress = IPAddress.Parse("0.0.0.0"); //Dns.GetHostEntry("localhost").AddressList[0];
            try
            {
                // Set the listener on the local IP address 
                // and specify the port.
                tcpListener = new TcpListener(ipAddress, 8000);
                tcpListener.Start();
                output = "Waiting for a connection...";
                Console.WriteLine(output);
            }
            catch (Exception e)
            {
                output = "Error: " + e.ToString();
                MessageBox.Show(output);
            }
            while (true)
            {
                // Always use a Sleep call in a while(true) loop 
                // to avoid locking up your CPU.
                Thread.Sleep(10);
                // Create a TCP socket. 
                // If you ran this server on the desktop, you could use 
                //Socket socket = tcpListener.AcceptSocket();
                Console.WriteLine("zabatit");
                // for greater flexibility.
                TcpClient tcpClient = tcpListener.AcceptTcpClient();
                // Read the data stream from the client. 
                Console.WriteLine("zabatit1");
                byte[] bytes = new byte[256];
                NetworkStream stream = tcpClient.GetStream();
                stream.Read(bytes, 0, bytes.Length);
                Console.WriteLine("zabatit2");
                Image ii = Image.FromStream(stream);
                ii.Size.Width.Equals(12);
                ii.Size.Height.Equals(12);
    
                PictureBox PictureBox1 = new PictureBox();
                Console.WriteLine("zabatit3");
                pictureBox1.Image = ii;
                pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
                ii.Save(@"c:\users\ibrahim\desktop\testing1.jpg");
                pictureBox1.Location = new Point(390, 50);
                this.Controls.Add(PictureBox1);
                          
                //SocketHelper helper = new SocketHelper();
                // helper.processMsg(tcpClient, stream, bytes);
            }
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            this.createListener();
        }
    }
    

    }

    -
    Edité par bassilaibrahim 26 décembre 2014 à 15:23:23

    • Partager sur Facebook
    • Partager sur Twitter

    network streaming c#

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown