win7中c#调用电脑摄像头拍照

 我来答
leichenfeng
2013-08-03 · TA获得超过757个赞
知道小有建树答主
回答量:425
采纳率:0%
帮助的人:479万
展开全部
请先安装Microsoft DirectX SDK

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 System.Runtime.InteropServices;

namespace 调用摄像头
{
public partial class Form1 : Form
{

cVideo video;
public Form1()
{
InitializeComponent();
}
public class VideoAPI //视频API类
{

// 视频API调用

[DllImport("avicap32.dll")]

public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);

[DllImport("avicap32.dll")]

public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);

[DllImport("User32.dll")]

public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);

[DllImport("User32.dll")]

public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);

// 常量

public const int WM_USER = 0x400;

public const int WS_CHILD = 0x40000000;

public const int WS_VISIBLE = 0x10000000;

public const int SWP_NOMOVE = 0x2;

public const int SWP_NOZORDER = 0x4;

public const int WM_CAP_DRIVER_CONNECT = WM_USER + 10;

public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11;

public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER + 5;

public const int WM_CAP_SET_PREVIEW = WM_USER + 50;

public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;

public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45;

public const int WM_CAP_START = WM_USER;

public const int WM_CAP_SAVEDIB = WM_CAP_START + 25;

}

public class cVideo //视频类
{

private IntPtr lwndC; //保存无符号句柄

private IntPtr mControlPtr; //保存管理指示器

private int mWidth;

private int mHeight;

public cVideo(IntPtr handle, int width, int height)
{

mControlPtr = handle; //显示视频控件的句柄

mWidth = width; //视频宽度

mHeight = height; //视频高度

}

/// <summary>

/// 打开视频设备

/// </summary>

public void StartWebCam()
{

byte[] lpszName = new byte[100];

byte[] lpszVer = new byte[100];

VideoAPI.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100);

this.lwndC = VideoAPI.capCreateCaptureWindowA(lpszName, VideoAPI.WS_CHILD | VideoAPI.WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);

if (VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0))
{

VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEWRATE, 100, 0);

VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEW, true, 0);

}

}

/// <summary>

/// 关闭视频设备

/// </summary>

public void CloseWebcam()
{

VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_DRIVER_DISCONNECT, 0, 0);

}

/// <summary>

/// 拍照

/// </summary>

/// <param name="path">要保存bmp文件的路径</param>

public void GrabImage(IntPtr hWndC, string path)
{

IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);

VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());

}

}

private void button1_Click(object sender, EventArgs e)
{

video = new cVideo(pictureBox1.Handle, pictureBox1.Width, pictureBox1.Height);

video.StartWebCam();

}

private void button2_Click(object sender, EventArgs e)
{
video.CloseWebcam();

}

private void button3_Click(object sender, EventArgs e)
{
video.GrabImage(pictureBox1.Handle, "d:\\a.bmp");

}

}
}
追问
唯一缺陷是还有个视频源选择弹出,不过已经很给力了
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
深圳量子动力
2024-10-28 广告
摄像头人脸捕捉可找深圳量子动力双目摄像系统可通过无线方式进行面部捕捉,可以满足在家、工作室、直播间、专业动捕棚等多种场景的不同使用人群。支持Metahuman Animator解算,支持升级animator高精度表情方案... 点击进入详情页
本回答由深圳量子动力提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式