c#显示mjpg-stream流

 

上图为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);
        }
    }
}
 

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值