基本信息
源码名称:C#打开电脑摄像头
源码大小:0.68M
文件格式:.zip
开发语言:C#
更新时间:2020-09-29
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// Form1 handle.
/// </summary>
private int hHwnd;
public struct videohdr_tag
{
public byte[] lpData;
public int dwBufferLength;
public int dwBytesUsed;
public int dwTimeCaptured;
public int dwUser;
public int dwFlags;
public int[] dwReserved;
}
#region P/Invoke
[DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
[DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool capGetDriverDescriptionA(short wDriver, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszName, int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszVer, int cbVer);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool DestroyWindow(int hndw);
[DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport("vfw32.dll")]
public static extern string capVideoStreamCallback(int hwnd, videohdr_tag videohdr_tag);
[DllImport("vicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool capSetCallbackOnFrame(int hwnd, string s);
#endregion
#region 初始化窗体1并在面板中显示视频
/// <summary>
/// Initialize Form1 and display the video in a panel.
/// 初始化窗体1并在面板中显示视频
/// display 展示 显示
/// </summary>
/// <returns></returns>
private bool InitializeForm1() //初始化 计算机程序或者系统
{
bool ok = false;
int intWidth = this.pictureBox1.Width;
int intHeight = this.pictureBox1.Height;
int intDevice = 0;
string refDevice = intDevice.ToString();
//Create vedio and get the window handle.
hHwnd = Form1.capCreateCaptureWindowA(ref refDevice, 1342177280, 0, 0, 640, 480, this.pictureBox1.Handle.ToInt32(), 0);
if (Form1.SendMessage(hHwnd, 0x40a, intDevice, 0) > 0)
{
Form1.SendMessage(this.hHwnd, 0x435, -1, 0);
Form1.SendMessage(this.hHwnd, 0x434, 0x42, 0);
Form1.SendMessage(this.hHwnd, 0x432, -1, 0);
Form1.SetWindowPos(this.hHwnd, 1, 0, 0, intWidth, intHeight, 6);
ok = true;
}
else
{
Form1.DestroyWindow(this.hHwnd);
}
return ok;
}
#endregion
#region 应用程序运行,然后调用Form1直到成功
/// <summary>
/// App run, then invoke the Form1 till successfully.
/// 应用程序运行,然后调用Form1直到成功
/// till 直到
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CloseForm1()
{
if (this.hHwnd > 0)
{
Form1.SendMessage(this.hHwnd, 0x40b, 0, 0);
Form1.DestroyWindow(this.hHwnd);
}
}
#endregion
#region 关闭窗口时,销毁窗体1窗口
/// <summary>
/// when close window, destroy the Form1 window.
/// 关闭窗口时,销毁窗体1窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
CloseForm1();
}
#endregion
#region 当窗口大小改变时,调整窗体1图片的大小
/// <summary>
/// when window size changed, resize Form1 pic.
/// 当窗口大小改变时,调整窗体1图片的大小。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_SizeChanged(object sender, EventArgs e)
{
Form1.SetWindowPos(this.hHwnd, 1, 0, 0, this.Width, this.Height, 6);
}
#endregion
private void but_打开摄像头_Click(object sender, EventArgs e)
{
bool ok = false;
while (!ok)
{
ok = this.InitializeForm1();
System.Threading.Thread.Sleep(10);
}
}
private void but_关闭摄像头_Click(object sender, EventArgs e)
{
CloseForm1();
}
}
}