之前写过一篇文章,讲C#调用网络摄像头,只不过是创建的WPF应用。这篇文章介绍一下创建Windows窗体应用,调用摄像头。之前的文章可参考C#调用摄像头并访问西门子PLC以及在车间的可能性应用
步骤-1
创建窗体应用
右击引用,选择管理NuGet程序包(N)。
浏览处输入aforge,然后点击安装一下几个文件。
安装完成后如有图所示
步骤-2
编写窗体
界面组成如上,两个按钮,一个TextBox,一个图片pictureBox1控件,其中SizeMode选择StretchImage。
代码如下:
using AForge;using AForge.Video;namespace PLC_CAMEAR_WinForm{ public partial class Form1 : Form { private IVideoSource videoSource; public Form1() { InitializeComponent(); } private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone(); pictureBox1.Image = bitmap; } private void btn_Start_Click(object sender, EventArgs e) { string ConnectionString = "http://" + cameraIp.Text + "/axis-cgi/jpg/image.cgi"; videoSource = new JPEGStream(ConnectionString); videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); videoSource.Start(); } private void btn_Stop_Click(object sender, EventArgs e) { videoSource.SignalToStop(); if (videoSource != null && videoSource.IsRunning && pictureBox1.Image != null) { pictureBox1.Image.Dispose(); } } }}