我正在做一个 Windows 应用程序,并且需要在将其保存在单独的文件夹中之前生动地捕捉该人的照片
能够在图片框中获取实时视频,但不知道如何在按钮单击事件中以任何指定的格式保存它
这是代码..首先下载DLLRefrence并在Visual Studio中添加为参考..
DLL 链接 http://www.mediafire.com/?dxrnc433pfrp7mu
并将此代码复制粘贴到您的程序中
namespace WinFormCharpWebCam
{
partial class mainWinForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.imgVideo = new System.Windows.Forms.PictureBox();
this.imgCapture = new System.Windows.Forms.PictureBox();
this.bntStart = new System.Windows.Forms.Button();
this.bntStop = new System.Windows.Forms.Button();
this.bntContinue = new System.Windows.Forms.Button();
this.bntCapture = new System.Windows.Forms.Button();
this.bntSave = new System.Windows.Forms.Button();
this.bntVideoFormat = new System.Windows.Forms.Button();
this.bntVideoSource = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.imgVideo)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.imgCapture)).BeginInit();
this.SuspendLayout();
//
// imgVideo
//
this.imgVideo.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.imgVideo.Location = new System.Drawing.Point(55, 41);
this.imgVideo.Name = "imgVideo";
this.imgVideo.Size = new System.Drawing.Size(163, 160);
this.imgVideo.TabIndex = 0;
this.imgVideo.TabStop = false;
//
// imgCapture
//
this.imgCapture.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.imgCapture.Location = new System.Drawing.Point(242, 41);
this.imgCapture.Name = "imgCapture";
this.imgCapture.Size = new System.Drawing.Size(163, 160);
this.imgCapture.TabIndex = 1;
this.imgCapture.TabStop = false;
//
// bntStart
//
this.bntStart.Location = new System.Drawing.Point(55, 216);
this.bntStart.Name = "bntStart";
this.bntStart.Size = new System.Drawing.Size(41, 23);
this.bntStart.TabIndex = 2;
this.bntStart.Text = "Start";
this.bntStart.UseVisualStyleBackColor = true;
this.bntStart.Click += new System.EventHandler(this.bntStart_Click);
//
// bntStop
//
this.bntStop.Location = new System.Drawing.Point(102, 216);
this.bntStop.Name = "bntStop";
this.bntStop.Size = new System.Drawing.Size(49, 23);
this.bntStop.TabIndex = 3;
this.bntStop.Text = "Stop";
this.bntStop.UseVisualStyleBackColor = true;
this.bntStop.Click += new System.EventHandler(this.bntStop_Click);
//
// bntContinue
//
this.bntContinue.Location = new System.Drawing.Point(157, 216);
this.bntContinue.Name = "bntContinue";
this.bntContinue.Size = new System.Drawing.Size(61, 23);
this.bntContinue.TabIndex = 4;
this.bntContinue.Text = "Continue";
this.bntContinue.UseVisualStyleBackColor = true;
this.bntContinue.Click += new System.EventHandler(this.bntContinue_Click);
//
// bntCapture
//
this.bntCapture.Location = new System.Drawing.Point(242, 216);
this.bntCapture.Name = "bntCapture";
this.bntCapture.Size = new System.Drawing.Size(85, 23);
this.bntCapture.TabIndex = 5;
this.bntCapture.Text = "Capture Image";
this.bntCapture.UseVisualStyleBackColor = true;
this.bntCapture.Click += new System.EventHandler(this.bntCapture_Click);
//
// bntSave
//
this.bntSave.Location = new System.Drawing.Point(326, 216);
this.bntSave.Name = "bntSave";
this.bntSave.Size = new System.Drawing.Size(79, 23);
this.bntSave.TabIndex = 6;
this.bntSave.Text = "Save Image";
this.bntSave.UseVisualStyleBackColor = true;
this.bntSave.Click += new System.EventHandler(this.bntSave_Click);
//
// bntVideoFormat
//
this.bntVideoFormat.Location = new System.Drawing.Point(425, 100);
this.bntVideoFormat.Name = "bntVideoFormat";
this.bntVideoFormat.Size = new System.Drawing.Size(147, 23);
this.bntVideoFormat.TabIndex = 7;
this.bntVideoFormat.Text = "Video Format";
this.bntVideoFormat.UseVisualStyleBackColor = true;
this.bntVideoFormat.Click += new System.EventHandler(this.bntVideoFormat_Click);
//
// bntVideoSource
//
this.bntVideoSource.Location = new System.Drawing.Point(425, 129);
this.bntVideoSource.Name = "bntVideoSource";
this.bntVideoSource.Size = new System.Drawing.Size(147, 23);
this.bntVideoSource.TabIndex = 8;
this.bntVideoSource.Text = "Video Source";
this.bntVideoSource.UseVisualStyleBackColor = true;
this.bntVideoSource.Click += new System.EventHandler(this.bntVideoSource_Click);
//
// mainWinForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(584, 362);
this.Controls.Add(this.bntVideoSource);
this.Controls.Add(this.bntVideoFormat);
this.Controls.Add(this.bntSave);
this.Controls.Add(this.bntCapture);
this.Controls.Add(this.bntContinue);
this.Controls.Add(this.bntStop);
this.Controls.Add(this.bntStart);
this.Controls.Add(this.imgCapture);
this.Controls.Add(this.imgVideo);
this.Name = "mainWinForm";
this.Text = "WinForm C# WebCam";
this.Load += new System.EventHandler(this.mainWinForm_Load);
((System.ComponentModel.ISupportInitialize)(this.imgVideo)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.imgCapture)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox imgVideo;
private System.Windows.Forms.PictureBox imgCapture;
private System.Windows.Forms.Button bntStart;
private System.Windows.Forms.Button bntStop;
private System.Windows.Forms.Button bntContinue;
private System.Windows.Forms.Button bntCapture;
private System.Windows.Forms.Button bntSave;
private System.Windows.Forms.Button bntVideoFormat;
private System.Windows.Forms.Button bntVideoSource;
}
}
using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
namespace WinFormCharpWebCam
{
public partial class mainWinForm : Form
{
public mainWinForm()
{
InitializeComponent();
}
WebCam webcam;
private void mainWinForm_Load(object sender, EventArgs e)
{
webcam = new WebCam();
webcam.InitializeWebCam(ref imgVideo);
}
private void bntStart_Click(object sender, EventArgs e)
{
webcam.Start();
}
private void bntStop_Click(object sender, EventArgs e)
{
webcam.Stop();
}
private void bntContinue_Click(object sender, EventArgs e)
{
webcam.Continue();
}
private void bntCapture_Click(object sender, EventArgs e)
{
imgCapture.Image = imgVideo.Image;
}
private void bntSave_Click(object sender, EventArgs e)
{
Helper.SaveImageCapture(imgCapture.Image);
}
private void bntVideoFormat_Click(object sender, EventArgs e)
{
webcam.ResolutionSetting();
}
private void bntVideoSource_Click(object sender, EventArgs e)
{
webcam.AdvanceSetting();
}
}
}
using System;
using System.IO;
using System.Linq;
using System.Text;
using WebCam_Capture;
using System.Collections.Generic;
namespace WinFormCharpWebCam
{
class WebCam
{
private WebCamCapture webcam;
private System.Windows.Forms.PictureBox _FrameImage;
private int FrameNumber = 30;
public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)
{
webcam = new WebCamCapture();
webcam.FrameNumber = ((ulong)(0ul));
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
_FrameImage = ImageControl;
}
void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
_FrameImage.Image = e.WebCamImage;
}
public void Start()
{
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.Start(0);
}
public void Stop()
{
webcam.Stop();
}
public void Continue()
{
// change the capture time frame
webcam.TimeToCapture_milliseconds = FrameNumber;
// resume the video capture from the stop
webcam.Start(this.webcam.FrameNumber);
}
public void ResolutionSetting()
{
webcam.Config();
}
public void AdvanceSetting()
{
webcam.Config2();
}
}
}
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WinFormCharpWebCam
{
class Helper
{
public static void SaveImageCapture(System.Drawing.Image image)
{
SaveFileDialog s = new SaveFileDialog();
s.FileName = "Image";// Default file name
s.DefaultExt = ".Jpg";// Default file extension
s.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension
// Show save file dialog box
// Process save file dialog box results
if (s.ShowDialog()==DialogResult.OK)
{
// Save Image
string filename = s.FileName;
FileStream fstream = new FileStream(filename, FileMode.Create);
image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
fstream.Close();
}
}
}
}
如果它工作......不要说谢谢......通过勾选它接受这个答案...... :)
我能够保存图像并得到预期的结果。
如果您在项目中需要此功能,请添加 .DLL 文件
只需检查以下 URL 即可下载代码
http://www.planet-source-code.com/vb/scripts/showcode.asp?lngWId=10&txtCodeId=1339