基本信息
源码名称:C# MJPG 网络摄像头例子源码下载
源码大小:0.27M
文件格式:.rar
开发语言:C#
更新时间:2015-05-10
   源码介绍



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;
using AForge.Video.DirectShow;

namespace MJPG
{
    public partial class Form1 : Form
    {
        private const int statLength = 15;
        private int statIndex = 0;
        private int statReady = 0;
        private int[] statCount = new int[statLength];
        public Form1()
        {
            InitializeComponent();        
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (videoSourcePlayer.VideoSource != null)
            {
                videoSourcePlayer.SignalToStop();
                videoSourcePlayer.WaitForStop();
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            URLForm form = new URLForm();
            form.Description = "请输入网络摄像头Snap的URL:";
            form.URLs = new string[]
                {
                 "http://192.168.131.73:8080/?action=snap",
                };

            if (form.ShowDialog(this) == DialogResult.OK)
            {

                JPEGStream jpegSource = new JPEGStream(form.URL);
                OpenVideoSource(jpegSource);
            }
         }

                private void toolStripMenuItem2_Click(object sender, EventArgs e)
                {
                    URLForm form = new URLForm();
                    form.Description = "请输入网络摄像头的 MJPEG 视频地址:";
                    form.URLs = new string[]
                   {
                      "http://192.168.131.73:8080/",
                    };
                form.ShowDialog();
                if (Class1.IsOpenOK==true)
                {

                    MJPEGStream mjpegSource = new MJPEGStream(form.URL   "?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;
            timer.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 fpsLabel_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


    }
}