Chat freely about anything...

User avatar
By hsuperandy
#41136 Hi all. I have a camera (now using Arducam OV2640) connected to ESP8266. I am trying to build something that sends websocket data between a C# application and ESP8266. The main purpose of this project is to build a robot, that can view the camera on the robot and control it using a C# application via Websocket.
However, the progress of my project is that it is able to send the command to the robot to make it move, but upon receiving the data from ESP8266, I am not able to piece the data into a proper video streaming to view on the windows C# application. I have used C# bitmap library but not able work properly. Any help is much appreciated. Below is what I have done so far:

Code: Select all        public async Task receiveData()
        {
            while(true)
            {
                ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[819200]);             

                WebSocketReceiveResult result = await ws.ReceiveAsync(buffer, CancellationToken.None);
                pbVideoFeed.Image = BytesToBitmap(buffer);
                if (ws.State != WebSocketState.Open)
                {
                    break;
                }
            }
        }

        private Bitmap BytesToBitmap(ArraySegment<byte> buffer)
        {
            MemoryStream stream = null;
            byte[] image_data = new byte[409600];
            Bitmap resize_img = new Bitmap(pbVideoFeed.Width, pbVideoFeed.Height);
            Graphics graphic = Graphics.FromImage(resize_img);

            image_data = buffer.ToArray();
            try
            {
                stream = new MemoryStream(image_data);
                Bitmap result = new Bitmap(stream);
                graphic.InterpolationMode = InterpolationMode.High;
                graphic.DrawImage(result, new Rectangle(0, 0, pbVideoFeed.Width, pbVideoFeed.Height));

                graphic.Dispose();

                return new Bitmap((Image)resize_img);
                //return new Bitmap((Image)new Bitmap(stream));
            }
            catch (ArgumentException ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
        }