C#如何打开摄像头

大虾们: 请问下怎么用C#打开电脑本身的摄像头,有没有相关方面的教学资料,先谢谢各位先~~~~~~~

第1个回答  2013-04-01
请先安装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");

}

}
}本回答被网友采纳
第2个回答  2013-04-02
public partial class Form1 : Form
{
const short WM_CAP = 1024;
const int WM_CAP_DRIVER_CONNECT = WM_CAP + 10;
const int WM_CAP_DRIVER_DISCONNECT = WM_CAP + 11;
const int WM_CAP_EDIT_COPY = WM_CAP + 30;
const int WM_CAP_SET_PREVIEW = WM_CAP + 50;
const int WM_CAP_SET_PREVIEWRATE = WM_CAP + 52;
const int WM_CAP_SET_SCALE = WM_CAP + 53;
const int WS_CHILD = 1073741824;
const int WS_VISIBLE = 268435456;
const short SWP_NOMOVE = 2;
const short SWP_NOSIZE = 1;
const short SWP_NOZORDER = 4;
const short HWND_BOTTOM = 1;
int iDevice = 0;
int hHwnd;
[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SendMessageA")]
static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)]
object lParam);
[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetWindowPos")]
static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[System.Runtime.InteropServices.DllImport("user32")]
static extern bool DestroyWindow(int hndw);
[System.Runtime.InteropServices.DllImport("avicap32.dll")]
static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
[System.Runtime.InteropServices.DllImport("avicap32.dll")]
static extern bool capGetDriverDescriptionA(short wDriver, string lpszName, int cbName, string lpszVer, int cbVer);
private void OpenPreviewWindow()
{
int iHeight = 320;
int iWidth = 200;
//
// Open Preview window in picturebox
//
hHwnd = capCreateCaptureWindowA(iDevice.ToString(), (WS_VISIBLE | WS_CHILD), 0, 0, 640, 480, picCapture.Handle.ToInt32(), 0);
//
// Connect to device
//
if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) == 1)
{
//
// Set the preview scale
//
SendMessage(hHwnd, WM_CAP_SET_SCALE, 1, 0);
//
// Set the preview rate in milliseconds
//
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);
//
// Start previewing the image from the camera
//
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, 0);
//
// Resize window to fit in picturebox
//
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, iWidth, iHeight, (SWP_NOMOVE | SWP_NOZORDER));
}
else
{
//
// Error connecting to device close window
//
DestroyWindow(hHwnd);
}
}
private void ClosePreviewWindow()
{
//
// Disconnect from device
//
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0);
//
// close window
//
DestroyWindow(hHwnd);
}

private void button1_Click(object sender, EventArgs e)
{
iDevice = int.Parse(device_number_textBox.Text);
OpenPreviewWindow();
}
}
第3个回答  2013-04-01
这个不清楚..还是百度google吧

c#获取网络摄像头打不开
1、打开开始菜单——控制面板。2、点击其中的FlashPlayer。3、在FlashPlayer设置管理器中点击切换到“摄像头和麦克风”。4、点击的“站点的摄像头和麦克风设置”。5、进入之后,点击左下角的“添加”按钮。6、输入需要使用摄像头的网站,点击“添加”。7、添加后可以在列表中看到该项,重新登录该网站,...

用C#如何实现启动摄像头?
安装像头后,一般可以找到一个avicap32.dll文件 这是一个关于像头的类 using system; using System.Runtime.InteropServices; namespace webcam { \/\/\/ \/\/\/ avicap 的摘要说明。 \/\/\/ public class showVideo { \/\/ showVideo calls [DllImport(avicap32.dll)] public static extern IntPtr capCreateCaptureWindow...

...知道 C#怎么 在窗体中单击按钮 就开打摄像头 出现图像 然后然后 照相...
需要调用硬件 比如说扫描器 摄像头等等 首先需要有相关硬件开发包 项目中引用该DLL文件 其次使用DLL相关方法即可调用,就是说DLL中 他已经写好给你直接调用的方法的 你就要去找对应的DLL文件

C#如何调用笔记本摄像头
1.调用USB摄像头 2.调用普通IP摄像头 3.通过摄像头SDK进行调用 http:\/\/blog.csdn.net\/crayfish_xujie\/article\/details\/46603189

怎么用c#调用外部摄像头
本文将讲诉在C#的winform程序中如何调用摄像头:1.调用USB摄像头 2.调用普通IP摄像头 3.通过摄像头SDK进行调用 http:\/\/blog.csdn.net\/crayfish_xujie\/article\/details\/46603189

C#怎么用代码打开电脑摄像头啊?
public partial class Form1 : Form { const short WM_CAP = 1024;const int WM_CAP_DRIVER_CONNECT = WM_CAP + 10;const int WM_CAP_DRIVER_DISCONNECT = WM_CAP + 11;const int WM_CAP_EDIT_COPY = WM_CAP + 30;const int WM_CAP_SET_PREVIEW = WM_CAP + 50;const int WM_CAP_...

c#中xamarin怎么连接海康摄像头添加照片
步骤如下:1.选择海康相机的IP地址,进行配置,点击网络,然后进入高级配置,集成协议,然后启用ONVIF添加用户类型。2.然后我们需要设置一个用户名和密码,以后会填入第三方硬盘录像机中。3.用户类型包括视频用户、操作用户和管理员。当然管理员的水平是最高的。经过我们的设置,可以在第三方硬盘录像机上...

c# emgu打开双目摄像头
你下载一些手电软件看看是否可以点亮闪光灯,如果问题依旧的话就要考虑硬件问题了,其次可以尝试系统还原,设置--通用--系统还原试一试,问题依旧基本可以断定是硬件问题,直接售后吧.

win7中c#调用电脑摄像头拍照,弹出"视频源"选择框,请问如何解决?
这是你没有释放资源造成的。你肯定是第一次打开的时候没有问题,然后关闭摄像头之后再打开,就出现这个问题了是吧?在你关闭摄像头的程序段里面加上释放资源的语句即可。

怎么才能用c#实现usb摄像头录像啊,急用
具体原因:" + ex.Message); return null; } }连接摄像头 public VideoCaptureDevice VideoConnect(int deviceIndex = 0, int resolutionIndex = 0) { if (videoDevices.Count <= 0) return null; selectedDeviceIndex = deviceIndex; videoSource = new VideoCaptureDevice(...

相似回答