AForge.Net框架本地视频和录制 代码与实例     using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using AForge.Video.DirectShow;using AForge.Video.FFMPEG;using AForge.Controls;using AForge.Video;using System.Net.Sockets;using System.Net;using System.Threading;using AForge.Imaging;using System.Drawing.Imaging;using System.IO;namespace AForge.Net框架本地视频和录制{  public partial class MainForm : Form  {    public MainForm()    {      InitializeComponent();      Form.CheckForIllegalCrossThreadCalls = false;    }    private Socket socket = null;    private Thread thread = null;    private Thread threadReceivePicture = null;    private AutoResetEvent autoRest = new AutoResetEvent(false);    private VideoCaptureDevice device = null;    private VideoFileWriter write = new VideoFileWriter();    private IPEndPoint remoteEndPoint = null;    private VideoCaptureDevice deviceTemp = null;    //打开摄像头    private void btnOpenVideoSoundPlayer_Click(object sender, EventArgs e)    {      VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();      if (form.ShowDialog(this) != DialogResult.OK) return;      this.device = form.VideoDevice;      this.videoSourcePlayer1.VideoSource = form.VideoDevice;      this.videoSourcePlayer1.Start();    }    //窗体关闭时关闭摄像头    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)    {      if (this.videoSourcePlayer1.IsRunning)      {        this.videoSourcePlayer1.Stop();        if (this.deviceTemp != null)        {          this.deviceTemp.SignalToStop();        }        if (this.device != null)        {          this.device.SignalToStop();        }      }    }    //停止视频    private void btnCloseVideoSoundPlayer_Click(object sender, EventArgs e)    {      if (this.videoSourcePlayer1.IsRunning)      {        this.videoSourcePlayer1.Stop();        if (this.deviceTemp !=null)        {          this.deviceTemp.SignalToStop();        }        if (this.device !=null)        {          this.device.SignalToStop();        }      }    }    //开始录像    private void btnStartRecord_Click(object sender, EventArgs e)    {      SaveFileDialog dialog = new SaveFileDialog();      dialog.Filter = "Flash Video(*.flv)|*.flv";      if (dialog.ShowDialog() != DialogResult.OK) return;      if (!this.videoSourcePlayer1.IsRunning)      {        VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();        if (form.ShowDialog(this) != DialogResult.OK) return;        this.device = form.VideoDevice;        this.device.NewFrame = device_NewFrame;        this.videoSourcePlayer1.VideoSource = form.VideoDevice;        this.device.Start();        this.videoSourcePlayer1.Start();        this.write.Open(dialog.FileName, 640, 480, 8, VideoCodec.FLV1);      }      else {        MessageBox.Show("设备正在运行,请先关闭设备!");      }    }    void device_NewFrame(object sender, Video.NewFrameEventArgs eventArgs)    {      Bitmap bitmap = eventArgs.Frame;      this.write.WriteVideoFrame(bitmap);    }    //停止录像    private void btnStopRecord_Click(object sender, EventArgs e)    {      this.device.SignalToStop();      this.device.WaitForStop();      this.videoSourcePlayer1.Stop();      this.write.Close();    }    //播放视频    private void btnStartPlayer_Click(object sender, EventArgs e)    {      OpenFileDialog open = new OpenFileDialog();      open.Filter = "Flash Video(*.flv)|*.flv";      if (open.ShowDialog() != DialogResult.OK) return;      VideoFileSource source = new VideoFileSource(open.FileName);      //source.Start();      this.videoSourcePlayer1.VideoSource = source;      this.videoSourcePlayer1.Start();    }    //停止播放    private void btnStopPlayer_Click(object sender, EventArgs e)    {      this.videoSourcePlayer1.Stop();    }    //初始化本地端点、Socket、监听线程    private void MainForm_Load(object sender, EventArgs e)    {      IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());      IPAddress ip = host.AddressList[1];      this.tbLoaclIp.Text = ip.ToString();      this.tbRemoteIp.Text = ip.ToString();      Random rand = new Random();      this.tbLocalPort.Text = rand.Next(20000 , 30000).ToString();            this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);      uint IOC_IN = 0x80000000;      uint IOC_VENDOR = 0x18000000;      uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;      socket.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);      this.socket.Bind(new IPEndPoint(IPAddress.Parse(this.tbLoaclIp.Text),int.Parse(this.tbLocalPort.Text)));      this.thread = new Thread(new ThreadStart(listenning));      this.thread.IsBackground = true;      this.thread.Start();    }    //接受图片的缓冲    private byte[] buffTemp = new byte[2 * 1024 * 1024];    //private byte[] buff = null;    //监听Socket    private void listenning()    {      while (true)      {        //每隔5000微妙查询一次socket状态        if (this.socket.Poll(5000,SelectMode.SelectRead))//为可读时,读取        {          this.socket.BeginReceive(buffTemp, 0, buffTemp.Length, SocketFlags.None, ReceiveFrame, this.socket);        }      }    }    //读取接受的图片并播放    private void ReceiveFrame(IAsyncResult ar)    {      Socket socketTemp = (Socket)ar.AsyncState;      int count = socketTemp.EndReceive(ar);      if (count>0)      {        byte[] buff = new byte[count];        Buffer.BlockCopy(buffTemp, 0, buff, 0, count);        MemoryStream memoryStream = new MemoryStream(buff);        Bitmap bitmap = (Bitmap)System.Drawing.Image.FromStream(memoryStream);        this.pictureBox1.Image = bitmap;      }    }    private void btnStartVideoCall_Click(object sender, EventArgs e)    {      if (this.tbRemoteIp.Text == "" || this.tbRemotePort.Text == "") return;      if (!this.videoSourcePlayer1.IsRunning)      {        VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();        if (form.ShowDialog(this) != DialogResult.OK) return;        //deviceTemp = form.VideoDevice;        //deviceTemp.NewFrame = deviceTemp_NewFrame;        this.remoteEndPoint = new IPEndPoint(IPAddress.Parse(this.tbRemoteIp.Text), int.Parse(this.tbRemotePort.Text));        this.videoSourcePlayer1.VideoSource = form.VideoDevice;        this.videoSourcePlayer1.NewFrame = videoSourcePlayer1_NewFrame;        this.videoSourcePlayer1.Start();        //threadTemp = new Thread(new ThreadStart(ThreadToSendImage));        //threadTemp.IsBackground = true;        //threadTemp.Start();        //deviceTemp.Start();      }      else      {        MessageBox.Show("设备正在使用中,请先关闭视频设备!");      }    }    void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)    {      fuction(image);    }    private object obcetBitmapTemp = new object();    private void fuction(Bitmap bitmap)    {      lock (obcetBitmapTemp)      {        //this.pictureBox1.Image = bitmap;        System.Drawing.Image imgTemp = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), bitmap.PixelFormat);        MemoryStream stream = new MemoryStream();        imgTemp.Save(stream, ImageFormat.Jpeg);        stream.Position = 0;        byte[] buffImage = new byte[stream.Length];        stream.Read(buffImage, 0, buffImage.Length);        this.socket.BeginSendTo(buffImage, 0, buffImage.Length, SocketFlags.None, this.remoteEndPoint, SendData, this.socket);        stream.Dispose();        stream = null;      }    }    private void SendData(IAsyncResult ar)    {      Socket socket = (Socket)ar.AsyncState;      socket.EndSendTo(ar);    }    //private void threadToReceivePicture()    //{    //  while (true)    //  {    //    this.autoRest.WaitOne();    //    this.pictureBox1.Image = this.bitmap;     //  }    //}    //private void ThreadToSendImage()    //{    //  while (true)    //  {    //    eventTemp.WaitOne();            //    //if (stream.Length <= 0) continue;    //    //this.pictureBox1.Image.Save(memoryStreamToSend, ImageFormat.Jpeg);    //    //stream.Position = 0;    //    //byte[] buffImage = new byte[stream.Length];    //    //stream.Read(buffImage, 0, buffImage.Length);    //    //this.socket.SendTo(buffImage, this.remoteEndPoint);     //    //stream.Dispose();    //    //stream = null;            //    //byte[] buffImage = new byte[(int)memoryStreamToSend.Length];    //    //memoryStreamToSend.Read(buffImage, 0, (int)memoryStreamToSend.Length);    //    //this.socket.BeginSendTo(buffImage, 0, buffImage.Length, SocketFlags.None, this.remoteEndPoint, SendData, this.socket);    //    //memoryStreamToSend.Position = 0;    //    //memoryStreamToSend = null;    //    this.listBox1.Items.Add("send");    //    //this.socket.SendTo(buffImage, this.remoteEndPoint);    //  }    //}  }}
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using AForge.Video.DirectShow;using AForge.Video.FFMPEG;using AForge.Controls;using AForge.Video;using System.Net.Sockets;using System.Net;using System.Threading;using AForge.Imaging;using System.Drawing.Imaging;using System.IO;namespace AForge.Net框架本地视频和录制{  public partial class MainForm : Form  {    public MainForm()    {      InitializeComponent();      Form.CheckForIllegalCrossThreadCalls = false;    }    private Socket socket = null;    private Thread thread = null;    private Thread threadReceivePicture = null;    private AutoResetEvent autoRest = new AutoResetEvent(false);    private VideoCaptureDevice device = null;    private VideoFileWriter write = new VideoFileWriter();    private IPEndPoint remoteEndPoint = null;    private VideoCaptureDevice deviceTemp = null;    //打开摄像头    private void btnOpenVideoSoundPlayer_Click(object sender, EventArgs e)    {      VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();      if (form.ShowDialog(this) != DialogResult.OK) return;      this.device = form.VideoDevice;      this.videoSourcePlayer1.VideoSource = form.VideoDevice;      this.videoSourcePlayer1.Start();    }    //窗体关闭时关闭摄像头    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)    {      if (this.videoSourcePlayer1.IsRunning)      {        this.videoSourcePlayer1.Stop();        if (this.deviceTemp != null)        {          this.deviceTemp.SignalToStop();        }        if (this.device != null)        {          this.device.SignalToStop();        }      }    }    //停止视频    private void btnCloseVideoSoundPlayer_Click(object sender, EventArgs e)    {      if (this.videoSourcePlayer1.IsRunning)      {        this.videoSourcePlayer1.Stop();        if (this.deviceTemp !=null)        {          this.deviceTemp.SignalToStop();        }        if (this.device !=null)        {          this.device.SignalToStop();        }      }    }    //开始录像    private void btnStartRecord_Click(object sender, EventArgs e)    {      SaveFileDialog dialog = new SaveFileDialog();      dialog.Filter = "Flash Video(*.flv)|*.flv";      if (dialog.ShowDialog() != DialogResult.OK) return;      if (!this.videoSourcePlayer1.IsRunning)      {        VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();        if (form.ShowDialog(this) != DialogResult.OK) return;        this.device = form.VideoDevice;        this.device.NewFrame = device_NewFrame;        this.videoSourcePlayer1.VideoSource = form.VideoDevice;        this.device.Start();        this.videoSourcePlayer1.Start();        this.write.Open(dialog.FileName, 640, 480, 8, VideoCodec.FLV1);      }      else {        MessageBox.Show("设备正在运行,请先关闭设备!");      }    }    void device_NewFrame(object sender, Video.NewFrameEventArgs eventArgs)    {      Bitmap bitmap = eventArgs.Frame;      this.write.WriteVideoFrame(bitmap);    }    //停止录像    private void btnStopRecord_Click(object sender, EventArgs e)    {      this.device.SignalToStop();      this.device.WaitForStop();      this.videoSourcePlayer1.Stop();      this.write.Close();    }    //播放视频    private void btnStartPlayer_Click(object sender, EventArgs e)    {      OpenFileDialog open = new OpenFileDialog();      open.Filter = "Flash Video(*.flv)|*.flv";      if (open.ShowDialog() != DialogResult.OK) return;      VideoFileSource source = new VideoFileSource(open.FileName);      //source.Start();      this.videoSourcePlayer1.VideoSource = source;      this.videoSourcePlayer1.Start();    }    //停止播放    private void btnStopPlayer_Click(object sender, EventArgs e)    {      this.videoSourcePlayer1.Stop();    }    //初始化本地端点、Socket、监听线程    private void MainForm_Load(object sender, EventArgs e)    {      IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());      IPAddress ip = host.AddressList[1];      this.tbLoaclIp.Text = ip.ToString();      this.tbRemoteIp.Text = ip.ToString();      Random rand = new Random();      this.tbLocalPort.Text = rand.Next(20000 , 30000).ToString();            this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);      uint IOC_IN = 0x80000000;      uint IOC_VENDOR = 0x18000000;      uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;      socket.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);      this.socket.Bind(new IPEndPoint(IPAddress.Parse(this.tbLoaclIp.Text),int.Parse(this.tbLocalPort.Text)));      this.thread = new Thread(new ThreadStart(listenning));      this.thread.IsBackground = true;      this.thread.Start();    }    //接受图片的缓冲    private byte[] buffTemp = new byte[2 * 1024 * 1024];    //private byte[] buff = null;    //监听Socket    private void listenning()    {      while (true)      {        //每隔5000微妙查询一次socket状态        if (this.socket.Poll(5000,SelectMode.SelectRead))//为可读时,读取        {          this.socket.BeginReceive(buffTemp, 0, buffTemp.Length, SocketFlags.None, ReceiveFrame, this.socket);        }      }    }    //读取接受的图片并播放    private void ReceiveFrame(IAsyncResult ar)    {      Socket socketTemp = (Socket)ar.AsyncState;      int count = socketTemp.EndReceive(ar);      if (count>0)      {        byte[] buff = new byte[count];        Buffer.BlockCopy(buffTemp, 0, buff, 0, count);        MemoryStream memoryStream = new MemoryStream(buff);        Bitmap bitmap = (Bitmap)System.Drawing.Image.FromStream(memoryStream);        this.pictureBox1.Image = bitmap;      }    }    private void btnStartVideoCall_Click(object sender, EventArgs e)    {      if (this.tbRemoteIp.Text == "" || this.tbRemotePort.Text == "") return;      if (!this.videoSourcePlayer1.IsRunning)      {        VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();        if (form.ShowDialog(this) != DialogResult.OK) return;        //deviceTemp = form.VideoDevice;        //deviceTemp.NewFrame = deviceTemp_NewFrame;        this.remoteEndPoint = new IPEndPoint(IPAddress.Parse(this.tbRemoteIp.Text), int.Parse(this.tbRemotePort.Text));        this.videoSourcePlayer1.VideoSource = form.VideoDevice;        this.videoSourcePlayer1.NewFrame = videoSourcePlayer1_NewFrame;        this.videoSourcePlayer1.Start();        //threadTemp = new Thread(new ThreadStart(ThreadToSendImage));        //threadTemp.IsBackground = true;        //threadTemp.Start();        //deviceTemp.Start();      }      else      {        MessageBox.Show("设备正在使用中,请先关闭视频设备!");      }    }    void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)    {      fuction(image);    }    private object obcetBitmapTemp = new object();    private void fuction(Bitmap bitmap)    {      lock (obcetBitmapTemp)      {        //this.pictureBox1.Image = bitmap;        System.Drawing.Image imgTemp = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), bitmap.PixelFormat);        MemoryStream stream = new MemoryStream();        imgTemp.Save(stream, ImageFormat.Jpeg);        stream.Position = 0;        byte[] buffImage = new byte[stream.Length];        stream.Read(buffImage, 0, buffImage.Length);        this.socket.BeginSendTo(buffImage, 0, buffImage.Length, SocketFlags.None, this.remoteEndPoint, SendData, this.socket);        stream.Dispose();        stream = null;      }    }    private void SendData(IAsyncResult ar)    {      Socket socket = (Socket)ar.AsyncState;      socket.EndSendTo(ar);    }    //private void threadToReceivePicture()    //{    //  while (true)    //  {    //    this.autoRest.WaitOne();    //    this.pictureBox1.Image = this.bitmap;     //  }    //}    //private void ThreadToSendImage()    //{    //  while (true)    //  {    //    eventTemp.WaitOne();            //    //if (stream.Length <= 0) continue;    //    //this.pictureBox1.Image.Save(memoryStreamToSend, ImageFormat.Jpeg);    //    //stream.Position = 0;    //    //byte[] buffImage = new byte[stream.Length];    //    //stream.Read(buffImage, 0, buffImage.Length);    //    //this.socket.SendTo(buffImage, this.remoteEndPoint);     //    //stream.Dispose();    //    //stream = null;            //    //byte[] buffImage = new byte[(int)memoryStreamToSend.Length];    //    //memoryStreamToSend.Read(buffImage, 0, (int)memoryStreamToSend.Length);    //    //this.socket.BeginSendTo(buffImage, 0, buffImage.Length, SocketFlags.None, this.remoteEndPoint, SendData, this.socket);    //    //memoryStreamToSend.Position = 0;    //    //memoryStreamToSend = null;    //    this.listBox1.Items.Add("send");    //    //this.socket.SendTo(buffImage, this.remoteEndPoint);    //  }    //}  }}

 
  
					
				
评论