如何使用C#Windows窗体在内部网络中创建网络摄像头聊天 [英] How Can I create a webcam chat in internal network using C# windows forms

查看:106
本文介绍了如何使用C#Windows窗体在内部网络中创建网络摄像头聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#Windows窗体在内部网络中创建网络摄像头聊天
您好,您能帮我创建一个网络摄像头聊天应用程序吗,我尝试使用套接字,但是遇到诸如缓冲区大小不匹配等问题..
我将tcpclinet和tcplistenert放在计时器函数(tick1)中.
第一部分接收图像作为字节数组并将其转换为图像.
第二部分发送图像并将其转换为字节数组.
我真的需要你的帮助.
我使用名为WebCamLib.dll的dll将网络摄像头中的图像预览到图片框中,虽然确实可以工作,但是我认为套接字部分存在问题.
我有两个图画盒.第一个预览来自网络摄像头的图片...第二个预览来自clint的图片.
这是我使用的代码(C#代码):

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):

<br />
<pre lang="css">static Byte[] bufferr { get; set; }<br />
       static Socket sok;<br />
       Image aa;<br />
       bool time = false;</pre><br />
<br />


1-将字节转换为图像数组


1- Convert byte to image array

<pre lang="cs">public Byte[] imageToByteArray(System.Drawing.Image imageIn)<br />
        {<br />
            MemoryStream ms = new MemoryStream();<br />
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);<br />
            return ms.ToArray();<br />
        }</pre><br />



2-在tick1函数中使用套接字的发送和接收部分
接收:



2-The Send And The receive part using sockets in tick1 function
Receive :

<pre lang="cs">private void timer1_Tick(object sender, EventArgs e)<br />
       {<br />
           try<br />
           {<br />
               sok = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);<br />
               sok.Bind(new IPEndPoint(0, 1993));<br />
<br />
               sok.Listen(100);<br />
               Socket acc = sck.Accept();<br />
               bufferr = new Byte[acc.sendbuffersize];<br />
               acc.Receive(bufferr, bufferr.Length, SocketFlags.None);<br />
               if (acc.Connected)<br />
               {<br />
                   label2.Text = "ONLINE";<br />
                   label2.ForeColor = Color.Green;<br />
                   pictureBox1.Image = ByteArrayToImage(bufferr);<br />
                   aa = pictureBox2.Image;<br />
                   pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;<br />
               }<br />
               sck.Close();<br />
               acc.Close();<br />
           }<br />
           catch<br />
           {<br />
           }</pre><br />


发送(也具有与接收部分相同的功能):


Send (also in the same function with the receive part) :

<pre lang="cs">/<br />
/===========================================================================<br />
                //SEND PART =================================================================<br />
                //===========================================================================<br />
            sck = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);<br />
            IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("192.168.0.2"),1993);<br />
            try<br />
            {<br />
                sck.Connect(ipe);<br />
                send = true;<br />
            }<br />
            catch<br />
            {<br />
                send = false;<br />
            }<br />
<br />
            if (send)<br />
            {<br />
               sck.Send(imageToByteArray(pictureBox1.Image), imageToByteArray(pictureBox1.Image).Length, SocketFlags.None);<br />
            }</pre><br />


3-将图像转换为字节数组:


3- Convert Image to byte array :

public Image ByteArrayToImage(Byte[] ByteArrayIn)
       {
           MemoryStream ms = new MemoryStream(ByteArrayIn);
           Image returnImage = Image.FromStream(ms);
           return returnImage;
       }


4-按钮1(令人震惊)开始从网络摄像头将照片拍摄到picturebox1


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-选择网络摄像头组合框




5- Select webcam combobox

<code><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.

推荐答案

查看所有

这篇关于如何使用C#Windows窗体在内部网络中创建网络摄像头聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆