c#第三次作业
本次实验要实现对摄像头的调用,主要是需要调用相应的API。在实验过程中遇到了因为系统设置了隐私问题无法调用的情况,需要根据各个品牌的实际情况进行一些调整。
- 程序设计题一:在很多的程序界面中,都是以菜单或工具栏的形式显示窗体界面,这种显示方式是以静止状态显示的,界面不够生动。请实现一个以动画显示窗体界面的设计方法;
核心代码:
namespace Demo3_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void axAnimation1_Enter(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
axAnimation1.Open(Application.StartupPath + @"\aaa.avi");
}
}
}
- 程序设计题2:利用普通的简易摄像头,通过C#语言即可开发成简易视频程序。请使用笔记本的普通摄像头,利用VFW技术,实现单路视频监控系统;
核心代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Demo3_2
{
public partial class Form1 : Form
{
cVideo video;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
btn_stop.Enabled = false;
video = new cVideo(pic_show.Handle, pic_show.Width, pic_show.Height);
video.StartWebCam();
}
private void button1_Click(object sender, EventArgs e)
{
btn_play.Enabled = false;
btn_stop.Enabled = true;
video.StarKinescope(@"d:\lx.avi");
}
private void btn_stop_Click(object sender, EventArgs e)
{
btn_play.Enabled = true;
btn_stop.Enabled = false;
video.StopKinescope();
}
private void btn_exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void pic_show_Click(object sender, EventArgs e)
{
}
}
}
- 程序设计题3:请使用摄像头来实现监控录像的程序并可对异常情况进行拍照。
核心代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Demo3_3;
namespace Demo3_3
{
public partial class Form1 : Form
{
WebCamera wc;
public Form1()
{
InitializeComponent();
}
private void btn_photo_Click(object sender, EventArgs e)
{
try
{
wc.GrabImage(this.panelPreview.Handle, "d://a.jpg");
MessageBox.Show("拍照成功!");
}
catch(Exception se)
{
MessageBox.Show("ERROR"+se.Message,"提示信息",MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
return;
}
}
private void Form1_Load(object sender, EventArgs e)
{
btn_play.Enabled = false;
btn_stop.Enabled = true;
panelPreview.Size = new Size(330, 330);
wc = new WebCamera(panelPreview.Handle, panelPreview.Width, panelPreview.Height);
wc.StartWebCam();
}
private void btn_play_Click(object sender, EventArgs e)
{
btn_play.Enabled = false;
btn_stop.Enabled = true;
panelPreview.Size = new Size(684, 552);
wc = new WebCamera(panelPreview.Handle, panelPreview.Width, panelPreview.Height);
wc.StartWebCam();
}
private void btn_stop_Click(object sender, EventArgs e)
{
btn_play.Enabled = true;
btn_stop.Enabled = false;
wc.CloseWebcam();
}
}
}
成功截图如下: