如
上图为Form1,主窗体。
引用AForge.Controls.dll后,工具箱出现控件,videosourceplayer就是我们要放到窗体中的视频流播放器控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Controls;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
namespace shujuku
{
public partial class window2 : UserControl
{
private const int statLength = 15;
private int statIndex = 0;
private int statReady = 0;
private int[] statCount = new int[statLength];
public window2()
{
InitializeComponent();
}
private void videoSourcePlayer1_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (videoSourcePlayer.VideoSource != null)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
}
}
private void button1_Click(object sender, EventArgs e)
{
MJPEGStream mjpegSource = new MJPEGStream("http://192.168.43.22:8080/?action=stream");
OpenVideoSource(mjpegSource);
Class1.IsOpenOK = false;
}
private void OpenVideoSource(IVideoSource source)
{
this.Cursor = Cursors.WaitCursor;
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
videoSourcePlayer.VideoSource = source;
videoSourcePlayer.Start();
statIndex = statReady = 0;
timer1.Start();
this.Cursor = Cursors.Default;
}
private void timer_Tick(object sender, EventArgs e)
{
IVideoSource videoSource = videoSourcePlayer.VideoSource;
if (videoSource != null)
{
statCount[statIndex] = videoSource.FramesReceived;
if (++statIndex >= statLength)
statIndex = 0;
if (statReady < statLength)
statReady++;
float fps = 0;
for (int i = 0; i < statReady; i++)
{
fps += statCount[i];
}
fps /= statReady;
statCount[statIndex] = 0;
// fpsLabel.Text = fps.ToString("F2") + " fps";
}
}
private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
{
DateTime now = DateTime.Now;
Graphics g = Graphics.FromImage(image);
SolidBrush brush = new SolidBrush(Color.Red);
g.DrawString(now.ToString(), this.Font, brush, new PointF(5, 5));
brush.Dispose();
g.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
JPEGStream jpegSource = new JPEGStream("http://192.168.43.22:8080/?action=snap");
OpenVideoSource(jpegSource);
}
}
}