c# - 在 Windows 应用程序中,如何使用 C#.Net 2.0 在图片框中保存来自网络摄像头的实时图像

标签 c# webcam

我在做一个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();

        }

    }
}
 }

如果它的工作......不要说谢谢......通过打勾接受这个答案......:)

关于c# - 在 Windows 应用程序中,如何使用 C#.Net 2.0 在图片框中保存来自网络摄像头的实时图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17989595/

相关文章:

c# - DataGridTemplateColumn 中的命令绑定(bind)

C# 网络摄像头库

c# - 如何拥有自动递增的版本号(Visual Studio)?

c# - 我可以使用哪些项目模板来创建 winform dll 库?

c# - 为用户锁定 asp.net 中的页面

c# - 使用 Asp.Net Boilerplate 进行单元测试时获取新的 DbContext

android:用于机器人应用程序的外部 usb 设备的 api

wpf - 使用 AForge.Net 在 WPF 应用程序上实现网络摄像头

java - 如何使用 Java 从网络摄像头获取视频和音频流?

c++ - 如何使用 DirectShow 和网络摄像头预览图像