在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → AForge.Net框架本地视频和录制播放

AForge.Net框架本地视频和录制播放

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:5.95M
  • 下载次数:102
  • 浏览次数:1029
  • 发布时间:2016-04-05
  • 实例类别:C#语言基础
  • 发 布 人:pandyer
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 视频 框架 录制 .NET AForge

实例介绍

【实例简介】AForge.Net框架本地视频和录制 代码与实例    

【实例截图】

【核心代码】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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);
        //    }
        //}
    }
}

实例下载地址

AForge.Net框架本地视频和录制播放

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 well_do 发表于: 2021-12-31 14:50 03
只支持.flv格式

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警