tijian_jichuang/Code/TiJianJieGuo/frm_cj.cs
2025-02-20 11:54:48 +08:00

101 lines
2.9 KiB
C#

using AForge.Video.DirectShow;
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 TiJianJieGuoGuanLi;
namespace TiJianJieGuo
{
public partial class frm_cj : Form
{
HotKeys h = new HotKeys();
frmYiShengJieGuoLuRuNew ysjglr;
public frm_cj()
{
InitializeComponent();
}
public frm_cj(frmYiShengJieGuoLuRuNew ysjglr1, string sb, string fbl) : this()
{
ysjglr = ysjglr1;
videoDevice = new VideoCaptureDevice(sb);
var videoCapabilities = videoDevice.VideoCapabilities;
int h = int.Parse(fbl.Split('x')[1]);
int w = int.Parse(fbl.Split('x')[0]);
this.Width = w;
this.Height = h;
foreach (var vc in videoCapabilities)
{
if (vc.FrameSize.Width == w && vc.FrameSize.Height == h)
videoDevice.VideoResolution = vc;
}
videoSourcePlayer.VideoSource = videoDevice;
videoSourcePlayer.Start();
}
VideoCaptureDevice videoDevice;
private void Disconnect()
{
if (videoSourcePlayer.VideoSource != null)
{
// stop video device
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
videoSourcePlayer.VideoSource = null;
if (videoDevice.ProvideSnapshots)
{
//videoDevice.SnapshotFrame -= new NewFrameEventHandler(videoDevice_SnapshotFrame);
}
//EnableConnectionControls(true);
}
}
private void frm_cj_Load(object sender, EventArgs e)
{
h.UnRegist(this.Handle, caiji);
h.Regist(this.Handle, (int)HotKeys.HotkeyModifiers.Control, Keys.Q, caiji);
}
private void caiji()
{
Bitmap bmp = null;
bmp = videoSourcePlayer.GetCurrentVideoFrame();
if (bmp == null)
{
MessageBox.Show("捕获图像失败!", "提示");
return;
}
else
{
if(CaiJied!=null)
{
CaiJied(bmp);
}
}
}
public event dele_caiji CaiJied;
private void frm_cj_FormClosing(object sender, FormClosingEventArgs e)
{
ysjglr.cj = null;
//h.UnRegist(this.Handle, caiji);
Disconnect();
}
protected override void WndProc(ref Message m)
{
h.ProcessHotKey(m);
base.WndProc(ref m);
}
}
public delegate void dele_caiji(Bitmap bm);
}