实例介绍
【实例简介】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);
// }
//}
}
}
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)