发个小程序希望有人需要(操作摄像头)

hhc123 2009-12-02 12:05:21
加精

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 WindowsFormsApplication24
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

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();
}
}
}


picCapture为一个PictureBox
device_number_textBox.Text=0为一个TextBox,值为0
...全文
32064 857 打赏 收藏 转发到动态 举报
写回复
用AI写文章
857 条回复
切换为时间正序
请发表友善的回复…
发表回复
aworldwith1 2012-08-15
  • 打赏
  • 举报
回复
还不错呦 学习下
xiaoyao1212121 2012-08-03
  • 打赏
  • 举报
回复
怎么用啊!
zeigeo 2012-06-09
  • 打赏
  • 举报
回复
有web程序调用摄像头的办法吗?求指教
Delta 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

楼主好人啊!
[/Quote]收藏了。。
lingyuzhishui 2012-04-22
  • 打赏
  • 举报
回复
可不可以介绍一下asp.net上怎么应用
q493486554 2012-04-11
  • 打赏
  • 举报
回复
C# 程序如何控制两个摄像头中任意一个呢? .
就是我有兩個攝像頭,一個用來作監控的,一個用來拍照和錄像的,我想在兩個攝像頭在打開運行時,能用程序控制兩個中的一個就可以了,哪位大俠可以指點下,小弟現在急需解決這個問題,先謝了!

q493486554 2012-04-11
  • 打赏
  • 举报
回复
[Quote=引用 814 楼 的回复:]
引用 79 楼 leo2003 的回复:
lz,
可以摄头像拍指定尺寸的相片么?

例如我把摄头像的影像放到一个 200*300 的picturebox,
然后加一个按钮“拍照”,
有什么办法把把这个picturebox中所显示的200*300图像保存出来??

我拍出来的,都是640*480,调不了。

请指教。

我目前就遇到这个问题了,拍出来的照片竟然是900kb,能……
[/Quote]


你在拍照保存的方法后面加上這句格式轉換就可以了,900KB轉為25KB
Bitmap m_bitmap = null;
m_bitmap = new Bitmap(picPathTemp);
//使用JPG格式壓縮保存
m_bitmap.Save(path, ImageFormat.Jpeg);

m_bitmap.Dispose();
System.IO.File.Delete(picPathTemp);
潜水VIP 2012-04-03
  • 打赏
  • 举报
回复
收藏~谢谢楼主了
牛哥_ 2012-04-01
  • 打赏
  • 举报
回复
好东西
lkenhy 2012-03-19
  • 打赏
  • 举报
回复
这代码好眼熟...貌似是老外写的
dali_yan 2012-03-11
  • 打赏
  • 举报
回复

显示该错误:
错误 2 找不到类型或命名空间名称“MarshalAsAttribute”(是否缺少 using 指令或程序集引用?) F:\my core_c#\TEMP_C#\WindowsFormsApplication24\WindowsFormsApplication24\Form1.cs 35 72 WindowsFormsApplication241
xieyonjie 2012-03-08
  • 打赏
  • 举报
回复
可以告诉我,这个小程序 可以适应的判断出分辨率吗?
snail_D_xiaolijian 2012-03-01
  • 打赏
  • 举报
回复
楼主 .. 我爱死你了 ..
yojinlin 2012-02-19
  • 打赏
  • 举报
回复
學習了。
图僧破 2012-02-16
  • 打赏
  • 举报
回复
刘明是个好同志
开发者孙小聪 2012-01-30
  • 打赏
  • 举报
回复
所有的摄像头都可以用么?
xiaoqiangailulu 2011-09-24
  • 打赏
  • 举报
回复
收藏 感谢
PrinceofFox 2011-09-24
  • 打赏
  • 举报
回复
真的假的...
Joson.e8love 2011-09-23
  • 打赏
  • 举报
回复
mark
lovesummerlau 2011-09-05
  • 打赏
  • 举报
回复
mark
加载更多回复(837)

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧