调用avicap32.dll第一次成功,以后都无法调用

requiem01119 2013-08-21 09:15:57
调用avicap32.dll第一次成功,以后都无法调用,picturebox里面剩下的都是黑色的,摄像头灯也没有亮,调用不成功。 还有一个问题是我的摄像头不能定位到中心位置。求大神帮忙。

下面是源码

这个是form1的文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;


namespace AutoCam
{
public partial class form1 : Form
{

public form1()
{
InitializeComponent();
}
Camera camera;

#region InitializeComponent

#region
btn_openCamera...
#endregion

private void button1_Click(object sender, EventArgs e)
{
this.btn_openCamera.Enabled = false;
this.btn_closeCam.Enabled = true;
this.btn_cameraPic.Enabled = true;
camera = new Camera(this.pic_camera.Handle, this.pic_camera.Width, this.pic_camera.Height);
camera.StartWebCam();
}
#endregion

#region btn_closeCam_Click
private void btn_closeCam_Click(object sender, EventArgs e)
{
this.btn_openCamera.Enabled = true;
this.btn_closeCam.Enabled = false;
this.btn_cameraPic.Enabled = false;
camera.CloseWebcam();
}
#endregion

#region btn_cameraPic_Click
private void btn_cameraPic_Click(object sender, EventArgs e)
{
camera.GrabImage(this.pic_camera.Handle, "guying.bmp");
}
#endregion

private void quit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}


这个是cameraAPI.cs的文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace AutoCam
{
class CameraAPI
{
[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_PREVIEWFORMAT = WM_USER + 45;
public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
public const int WM_CAP_START = WM_USER;
public const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
}
}


这个是camera.cs的class用来生成cameraAPI实例

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows;

namespace AutoCam
{

class Camera
{
private IntPtr lwndC;
private IntPtr mControlPtr;
private int mWidth;
private int mHeight;

public Camera(IntPtr handel, int width, int height)
{
mControlPtr = handel; //handle of video dom
mWidth = width; //video width
mHeight = height; //video height
}

public void StartWebCam()
{
byte[] lpszName = new byte[100];
byte[] lpszVer = new byte[100];
CameraAPI.capGetDriverDescriptionA(0, lpszName,100, lpszVer, 0);
this.lwndC = CameraAPI.capCreateCaptureWindowA(lpszName, CameraAPI.WS_CHILD | CameraAPI.WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);
if (CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_DRIVER_CONNECT, 0, 0))
{
CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SET_PREVIEWRATE, 100, 0);
CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SET_PREVIEW, true, 0);
}
}

public void CloseWebcam()
{
CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_DRIVER_DISCONNECT, 0, 0);
}

public void GrabImage(IntPtr hWndC, string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
}
}
}
...全文
210 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
timeflies6 2014-04-23
  • 打赏
  • 举报
回复
请问楼主解决了吗,我也遇到这样的问题
hyblusea 2013-08-22
  • 打赏
  • 举报
回复
参考我的一篇贴子:http://bbs.csdn.net/topics/390016900
requiem01119 2013-08-22
  • 打赏
  • 举报
回复
没人么???????????
requiem01119 2013-08-21
  • 打赏
  • 举报
回复

110,534

社区成员

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

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

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