Share via


Sending and receiving an image over sockets with C# | Screen Sharing Using C#

Question

Thursday, February 22, 2018 8:44 PM

I am trying to set up two programs in C#. Basically, a simple server side set up where I want the client to listen for an image from the Server. Then, upon receiving the image, will display it in a PictureBox.I keep running into the following error:A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll or Parameter is not ValidServer Side

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ServerComputer
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }
        Socket sendsocket;
        private void goLive_Click(object sender, EventArgs e)
        {
            try
            {
                sendsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //The instantiation of socket, IP for 192.168.1.106, 10001 for Port
                IPEndPoint ipendpiont = new IPEndPoint(IPAddress.Parse(ipAddress.Text.Trim()), 10001);
                sendsocket.Connect(ipendpiont);
                //Establishment of end point
                Thread th = new Thread(new ThreadStart(threadimage));
                th.IsBackground = true;
                th.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            this.Hide();    //Hidden form
        }
        private Bitmap GetScreen()
        {
            Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics g = Graphics.FromImage(bitmap);
            g.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
            return bitmap;
        }
        private void threadimage()
        {
            try
            {
                while (true)
                {
                    MemoryStream ms = new MemoryStream();
                    GetScreen().Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);   //Here I use the BMP format
                    byte[] b = ms.ToArray();
                    sendsocket.Send(b);
                    Thread.Sleep(67);     //I'm here to set to send a second
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                return;
            }
        }
    }
}

Client Side

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClientComputer
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }
        Socket hostSocket;
        Thread thread;
        string localIP = string.Empty;
        string computrHostName = string.Empty;
        private void mainForm_Load(object sender, EventArgs e)
        {
            computrHostName = Dns.GetHostName();
            IPHostEntry hostname = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in hostname.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip.ToString();
                }
            }
        }
        private void liveScreen_Click(object sender, EventArgs e)
        {
            Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint hostIpEndPoint = new IPEndPoint(IPAddress.Parse(localIP), 10001);
            //Connection node
            receiveSocket.Bind(hostIpEndPoint);
            receiveSocket.Listen(10);
            MessageBox.Show("start");
            hostSocket = receiveSocket.Accept();
            thread = new Thread(trreadimage);
            thread.Start();
            thread.IsBackground = true;
        }
        private void trreadimage()
        {
            int dataSize, i = 0;
            try
            {
                while (true)
                {
                    i++;
                    byte[] b = new byte[1024 * 1024 * 20];  //Picture of great
                    dataSize = hostSocket.Receive(b,0,b.Length,SocketFlags.None);
                    MemoryStream ms = new MemoryStream(b,0,dataSize,true);
                    //bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    Image img = Image.FromStream(ms);
                    img.Save("Image"+i+".Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    videoBox.Image = img;
                    Console.WriteLine("Image Size: " + dataSize);
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                thread.Abort();
            }
        }
    }
}

All replies (3)

Monday, February 26, 2018 8:34 AM âś…Answered | 1 vote

Hi Arsalan,

Following code is working now, and I have few suggestions,

Try Async methods/events to receive and send data through socket instead of using loop or recursive methods.

Server Code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace ServerComputer
{
    publicpartialclassmainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }
        Socket sendsocket;
        privatevoid goLive_Click(object sender, EventArgs e)
        {
            try
            {
                sendsocket = newSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //The instantiation of socket, IP for 192.168.1.106, 10001 for PortIPEndPoint ipendpiont = newIPEndPoint(IPAddress.Parse(ipAddress.Text.Trim()), 10001);
                sendsocket.Connect(ipendpiont);
                //Establishment of end pointThread th = newThread(newThreadStart(threadimage));
                th.IsBackground = true;
                th.Start();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                return;
            }
            this.Hide();    //Hidden form
        }
        privateBitmap GetScreen()
        {
            Bitmap bitmap = newBitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics g = Graphics.FromImage(bitmap);
            g.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
            return bitmap;
        }
        privatevoid threadimage()
        {
            try
            {
                MemoryStream ms = newMemoryStream();
                GetScreen().Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);   //Here I use the BMP formatbyte[] b = ms.ToArray();
 
                sendsocket.Send(b);
                ms.Close();
 
 
            }
            catch (Exception ee)
            {
                // MessageBox.Show(ee.Message);//return;
            }
 
            Thread.Sleep(1000);
            threadimage();
        }
    }
}
Clint Code.using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace ClientComputer
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }
        Socket hostSocket;
        Thread thread;
        string localIP = string.Empty;
        string computrHostName = string.Empty;
        private void mainForm_Load(object sender, EventArgs e)
        {
            computrHostName = Dns.GetHostName();
            IPHostEntry hostname = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in hostname.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip.ToString();
                }
            }
            this.Text = this.Text + " | " + localIP;
 
        }
        private void liveScreen_Click(object sender, EventArgs e)
        {
            connectSocket();
        }
 
        private void connectSocket()
        {
            Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint hostIpEndPoint = new IPEndPoint(IPAddress.Parse(localIP), 10001);
            //Connection node
            receiveSocket.Bind(hostIpEndPoint);
            receiveSocket.Listen(10);
            MessageBox.Show("start");
            hostSocket = receiveSocket.Accept();
            thread = new Thread(new ThreadStart(trreadimage));
 
            thread.IsBackground = true;
            thread.Start();
        }
        private void trreadimage()
        {
            int dataSize;
            string imageName = "Image-" + System.DateTime.Now.Ticks + ".JPG";
            try
            {
 
                dataSize = 0;
                byte[] b = new byte[1024 * 10000];  //Picture of great
                dataSize = hostSocket.Receive(b);
                if (dataSize > 0)
                {
                    MemoryStream ms = new MemoryStream(b);
                    Image img = Image.FromStream(ms);
                    img.Save(imageName, System.Drawing.Imaging.ImageFormat.Jpeg);
                    videoBox.Image = img;
                    ms.Close();
                }
 
            }
            catch (Exception ee)
            {
                //MessageBox.Show(ee.Message);
                //thread.Abort();
            }
            System.Threading.Thread.Sleep(1500);
            trreadimage();
        }
    }
}

                                                                                


Sunday, February 25, 2018 4:15 AM

Hi ArslanPervaiz,

Thank you for posting here.

According to the error message we know that the exception is thrown when one of the arguments provided to a method is not valid.

Did you try to check your code? which sentence throws the exception. Maybe you need to provide appropriate arguments

Best Regards,

Hart

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Monday, February 26, 2018 9:23 AM

Thanks You Sir...

It's worked for me :)