How Can I create a webcam chat in internal network using C# windows forms
Hello , can you help me to create a webcam chat application, i tried to use sockets but i had problems such as the size of the buffer didn't match and some other things ..
i put the tcpclinet and the tcplistenert in the timer function (tick1).
the first part receive an image as a byte array and convert it to image .
the second part send an image from converting it to byte array.
i really need your help.
i used dll named WebCamLib.dll to preview the images from the webcam into the picturebox and i really worked but i had problems the sockets part i think .
i have two picturew boxes . the first preview the picture from the webcam ... the second one preview the picture from the clint.
this is the code i used (C# Code):
static Byte[] bufferr { get; set; }
static Socket sok;
Image aa;
bool time = false;
1- Convert byte to image array
public Byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
2-The Send And The receive part using sockets in tick1 function
Receive :
private void timer1_Tick(object sender, EventArgs e)
{
try
{
sok = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sok.Bind(new IPEndPoint(0, 1993));
sok.Listen(100);
Socket acc = sck.Accept();
bufferr = new Byte[acc.sendbuffersize];
acc.Receive(bufferr, bufferr.Length, SocketFlags.None);
if (acc.Connected)
{
label2.Text = "ONLINE";
label2.ForeColor = Color.Green;
pictureBox1.Image = ByteArrayToImage(bufferr);
aa = pictureBox2.Image;
pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
}
sck.Close();
acc.Close();
}
catch
{
}
Send (also in the same function with the receive part) :
/
/===========================================================================
sck = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("192.168.0.2"),1993);
try
{
sck.Connect(ipe);
send = true;
}
catch
{
send = false;
}
if (send)
{
sck.Send(imageToByteArray(pictureBox1.Image), imageToByteArray(pictureBox1.Image).Length, SocketFlags.None);
}
3- Convert Image to byte array :
public Image ByteArrayToImage(Byte[] ByteArrayIn)
{
MemoryStream ms = new MemoryStream(ByteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
4- button 1 (startchating) start taking pictures to picturebox1 from a webcam
<pre lang="cs">private void button1_Click(object sender, EventArgs e)
{
Device d = DeviceManager.GetDevice(cmbDevices.SelectedIndex);
d.ShowWindow(this.pictureBox1);
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
timer1.Enabled = true;
}
5- Select webcam combobox
<pre lang="cs">private void Form1_Load(object sender, EventArgs e)
{
Device[] devices = DeviceManager.GetAllDevices();
foreach (Device d in devices)
{
cmbDevices.Items.Add(d);
}
if (devices.Length > 0)
{
cmbDevices.SelectedIndex = 0;
}
}
Thank you for reading and i really need a solution.