win0 搜狗输入法:摄像头编程源码-视频捕捉

来源:百度文库 编辑:中财网 时间:2024/04/26 16:59:23

摄像头编程

http://www.cnblogs.com/sbdx/archive/2006/07/19/454502.html

资料一
1 你要有视频捕捉设备(50元一个摄像头你应该有吧..)

2 正确安装在你的电脑上。

3 新建一个VB项目,添加一个Module

Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" _
  Alias "capCreateCaptureWindowA" ( _
  ByVal lpszWindowName As String, _
  ByVal dwStyle As Long, _
  ByVal x As Long, _
  ByVal y As Long, _
  ByVal nWidth As Long, _
  ByVal nHeight As Long, _
  ByVal hWndParent As Long, _
  ByVal nID As Long) As Long

Private Const WS_CHILD = &H40000000
Private Const WS_VISIBLE = &H10000000
Private Const WM_USER = &H400
Private Const WM_CAP_START = &H400
Private Const WM_CAP_EDIT_COPY = (WM_CAP_START + 30)
Private Const WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10)
Private Const WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52)
Private Const WM_CAP_SET_OVERLAY = (WM_CAP_START + 51)
Private Const WM_CAP_SET_PREVIEW = (WM_CAP_START + 50)
Private Const WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11)

Private Declare Function SendMessage Lib "user32" _
  Alias "SendMessageA" ( _
  ByVal hwnd As Long, _
  ByVal wMsg As Long, _
  ByVal wParam As Long, _
  lParam As Any) As Long

Private Preview_Handle As Long


Public Function CreateCaptureWindow( _
  hWndParent As Long, _
  Optional x As Long = 0, _
  Optional y As Long = 0, _
  Optional nWidth As Long = 320, _
  Optional nHeight As Long = 240, _
  Optional nCameraID As Long = 0) As Long

  Preview_Handle = capCreateCaptureWindow("Video", _
    WS_CHILD + WS_VISIBLE, x, y, _
    nWidth, nHeight, hWndParent, 1)

  SendMessage Preview_Handle, WM_CAP_DRIVER_CONNECT, nCameraID, 0
  SendMessage Preview_Handle, WM_CAP_SET_PREVIEWRATE, 30, 0
  SendMessage Preview_Handle, WM_CAP_SET_OVERLAY, 1, 0
  SendMessage Preview_Handle, WM_CAP_SET_PREVIEW, 1, 0

  CreateCaptureWindow = Preview_Handle
End Function


Public Function CapturePicture(nCaptureHandle As Long) As StdPicture
  Clipboard.Clear
  SendMessage nCaptureHandle, WM_CAP_EDIT_COPY, 0, 0
  Set CapturePicture = Clipboard.GetData
End Function


Public Sub Disconnect(nCaptureHandle As Long, _
  Optional nCameraID = 0)

  SendMessage nCaptureHandle, WM_CAP_DRIVER_DISCONNECT, _
    nCameraID, 0
End Sub

4 在form上添加一个PictureBox,一个按钮,Caption设为 Save Pic
Dim Video_Handle As Long

Private Sub Form_Load()
    Video_Handle = CreateCaptureWindow(PicCapture.hwnd)
End Sub

Private Sub Command1_Click()
    Dim x As StdPicture
    Set x = CapturePicture(Video_Handle)
    SavePicture x, "c:\a.bmp"
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Disconnect Video_Handle
End Sub

资料二
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" Alias "capCreateCaptureWindowA" (ByVal lpszWindowName As String, ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hwndParent As Long, ByVal nID As Long) As Long
Private Const GET_FRAME As Long = 1084
Private Const COPY As Long = 1054
Private Const CONNECT As Long = 1034
Private Const DISCONNECT As Long = 1035
Private CapHwnd As Long

Private Sub Command1_Click()
   '打开摄像头
   CapHwnd = capCreateCaptureWindow("WebcamCapture", 0, 0, 0, 640, 480, Me.hwnd, 0)
   DoEvents
   SendMessage CapHwnd, CONNECT, 0, 0
   Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
   '停止摄像头
   DoEvents
   SendMessage CapHwnd, DISCONNECT, 0, 0
   Timer1.Enabled = False
   
End Sub
Private Sub Form_Load()
'几句代码实现控制摄像头,有摄像头的进哦
    Combo1.AddItem "160*120"
    Combo1.AddItem "176*144"
    Combo1.AddItem "320*240"
    Combo1.AddItem "352*288"
    Combo1.AddItem "640*480"
    Combo1.ListIndex = 0
    
End Sub
Private Sub Timer1_Timer()  'timer1.Interval=50
   On Error Resume Next
   SendMessage CapHwnd, GET_FRAME, 0, 0
   SendMessage CapHwnd, COPY, 0, 0
   Picture1.Picture = Clipboard.GetData
   Picture2.Picture = Clipboard.GetData
   
   Clipboard.Clear
   
End Sub
资料三
****************************************************************
'* VB file: VFW.bas VB32 wrapper for Win32 Video For Windows
'* functions.
'* created: 1998 by Ray Mercer
'* modified: 12/02/98 by Ray Mercer (added comments)
'* last modified: 01/04/99 by Ray Mercer (changed capGetVideoFormat()
'* and capSetVideoFormat() macro wrappers to make them
'* easier to use)
'*
'* A Visual Basic translation of Microsoft's vfw.h file which is
'* part of the Win32 Platform SDK (VFW DDK)
'*
'* Download the latest version of this file at http://i.am/shrinkwrapvb
'*
'* Copyright (c) 1998-1999 by Ray Mercer. All rights reserved.
'****************************************************************

Option Explicit

'------------------------------------------------------------------
' Messages which can be sent to an AVICAP window
'------------------------------------------------------------------

Public Const WM_USER As Long = &H400
Public Const WM_CAP_START As Long = WM_USER

Public Const WM_CAP_GET_CAPSTREAMPTR As Long = WM_CAP_START + 1

Public Const WM_CAP_SET_CALLBACK_ERROR As Long = WM_CAP_START + 2
Public Const WM_CAP_SET_CALLBACK_STATUS As Long = WM_CAP_START + 3
Public Const WM_CAP_SET_CALLBACK_YIELD As Long = WM_CAP_START + 4
Public Const WM_CAP_SET_CALLBACK_FRAME As Long = WM_CAP_START + 5
Public Const WM_CAP_SET_CALLBACK_VIDEOSTREAM As Long = WM_CAP_START + 6
Public Const WM_CAP_SET_CALLBACK_WAVESTREAM As Long = WM_CAP_START + 7
Public Const WM_CAP_GET_USER_DATA As Long = WM_CAP_START + 8
Public Const WM_CAP_SET_USER_DATA As Long = WM_CAP_START + 9

Public Const WM_CAP_DRIVER_CONNECT As Long = WM_CAP_START + 10
Public Const WM_CAP_DRIVER_DISCONNECT As Long = WM_CAP_START + 11
Public Const WM_CAP_DRIVER_GET_NAME As Long = WM_CAP_START + 12
Public Const WM_CAP_DRIVER_GET_VERSION As Long = WM_CAP_START + 13
Public Const WM_CAP_DRIVER_GET_CAPS As Long = WM_CAP_START + 14

Public Const WM_CAP_FILE_SET_CAPTURE_FILE As Long = WM_CAP_START + 20
Public Const WM_CAP_FILE_GET_CAPTURE_FILE As Long = WM_CAP_START + 21
Public Const WM_CAP_FILE_ALLOCATE As Long = WM_CAP_START + 22
Public Const WM_CAP_FILE_SAVEAS As Long = WM_CAP_START + 23
Public Const WM_CAP_FILE_SET_INFOCHUNK As Long = WM_CAP_START + 24
Public Const WM_CAP_FILE_SAVEDIB As Long = WM_CAP_START + 25

Public Const WM_CAP_EDIT_COPY As Long = WM_CAP_START + 30

Public Const WM_CAP_SET_AUDIOFORMAT As Long = WM_CAP_START + 35
Public Const WM_CAP_GET_AUDIOFORMAT As Long = WM_CAP_START + 36

Public Const WM_CAP_DLG_VIDEOFORMAT As Long = WM_CAP_START + 41
Public Const WM_CAP_DLG_VIDEOSOURCE As Long = WM_CAP_START + 42
Public Const WM_CAP_DLG_VIDEODISPLAY As Long = WM_CAP_START + 43
Public Const WM_CAP_GET_VIDEOFORMAT As Long = WM_CAP_START + 44
Public Const WM_CAP_SET_VIDEOFORMAT As Long = WM_CAP_START + 45
Public Const WM_CAP_DLG_VIDEOCOMPRESSION As Long = WM_CAP_START + 46

Public Const WM_CAP_SET_PREVIEW As Long = WM_CAP_START + 50
Public Const WM_CAP_SET_OVERLAY As Long = WM_CAP_START + 51
Public Const WM_CAP_SET_PREVIEWRATE As Long = WM_CAP_START + 52
Public Const WM_CAP_SET_SCALE As Long = WM_CAP_START + 53
Public Const WM_CAP_GET_STATUS As Long = WM_CAP_START + 54
Public Const WM_CAP_SET_SCROLL As Long = WM_CAP_START + 55

Public Const WM_CAP_GRAB_FRAME As Long = WM_CAP_START + 60
Public Const WM_CAP_GRAB_FRAME_NOSTOP As Long = WM_CAP_START + 61

Public Const WM_CAP_SEQUENCE As Long = WM_CAP_START + 62
Public Const WM_CAP_SEQUENCE_NOFILE As Long = WM_CAP_START + 63
Public Const WM_CAP_SET_SEQUENCE_SETUP As Long = WM_CAP_START + 64
Public Const WM_CAP_GET_SEQUENCE_SETUP As Long = WM_CAP_START + 65
Public Const WM_CAP_SET_MCI_DEVICE As Long = WM_CAP_START + 66
Public Const WM_CAP_GET_MCI_DEVICE As Long = WM_CAP_START + 67
Public Const WM_CAP_STOP As Long = WM_CAP_START + 68
Public Const WM_CAP_ABORT As Long = WM_CAP_START + 69

Public Const WM_CAP_SINGLE_FRAME_OPEN As Long = WM_CAP_START + 70
Public Const WM_CAP_SINGLE_FRAME_CLOSE As Long = WM_CAP_START + 71
Public Const WM_CAP_SINGLE_FRAME As Long = WM_CAP_START + 72

Public Const WM_CAP_PAL_OPEN As Long = WM_CAP_START + 80
Public Const WM_CAP_PAL_SAVE As Long = WM_CAP_START + 81
Public Const WM_CAP_PAL_PASTE As Long = WM_CAP_START + 82
Public Const WM_CAP_PAL_AUTOCREATE As Long = WM_CAP_START + 83
Public Const WM_CAP_PAL_MANUALCREATE As Long = WM_CAP_START + 84

Public Const WM_CAP_SET_CALLBACK_CAPCONTROL As Long = WM_CAP_START + 85

' ------------------------------------------------------------------
' VFW UDTS (from vfw.h)
' ------------------------------------------------------------------

Type VFWPOINT 'strange name to avoid collision with other POINT UDTs
X As Long
Y As Long
End Type

Type CAPDRIVERCAPS
wDeviceIndex As Long '// Driver index in system.ini
fHasOverlay As Long '// Can device overlay?
fHasDlgVideoSource As Long '// Has Video source dlg?
fHasDlgVideoFormat As Long '// Has Format dlg?
fHasDlgVideoDisplay As Long '// Has External out dlg?
fCaptureInitialized As Long '// Driver ready to capture?
fDriverSuppliesPalettes As Long '// Can driver make palettes?
hVideoIn As Long '// Driver In channel
hVideoOut As Long '// Driver Out channel
hVideoExtIn As Long '// Driver Ext In channel
hVideoExtOut As Long '// Driver Ext Out channel
End Type

Type CAPSTATUS
uiImageWidth As Long '// Width of the image
uiImageHeight As Long '// Height of the image
fLiveWindow As Long '// Now Previewing video?
fOverlayWindow As Long '// Now Overlaying video?
fScale As Long '// Scale image to client?
ptScroll As VFWPOINT '// Scroll position
fUsingDefaultPalette As Long '// Using default driver palette?
fAudioHardware As Long '// Audio hardware present?
fCapFileExists As Long '// Does capture file exist?
dwCurrentVideoFrame As Long '// # of video frames cap'td
dwCurrentVideoFramesDropped As Long '// # of video frames dropped
dwCurrentWaveSamples As Long '// # of wave samples cap'td
dwCurrentTimeElapsedMS As Long '// Elapsed capture duration
hPalCurrent As Long '// Current palette in use
fCapturingNow As Long '// Capture in progress?
dwReturn As Long '// Error value after any operation
wNumVideoAllocated As Long '// Actual number of video buffers
wNumAudioAllocated As Long '// Actual number of audio buffers
End Type

Public Const AVSTREAMMASTER_AUDIO As Long = 0 '/* Audio master (VFW 1.0, 1.1) */
Public Const AVSTREAMMASTER_NONE As Long = 1 '/* No master */
Type CAPTUREPARMS
dwRequestMicroSecPerFrame As Long '// Requested capture rate
fMakeUserHitOKToCapture As Long '// Show "Hit OK to cap" dlg?
wPercentDropForError As Long '// Give error msg if > (10% default)
fYield As Long '// Capture via background task?
dwIndexSize As Long '// Max index size in frames (32K default)
'
wChunkGranularity As Long '// Junk chunk granularity (2K default)
'
fUsingDOSMemory As Long '// Use DOS buffers? (obsolete)
'
wNumVideoRequested As Long '// # video buffers, If 0, autocalc
fCaptureAudio As Long '// Capture audio?
'
wNumAudioRequested As Long '// # audio buffers, If 0, autocalc
'
vKeyAbort As Long '// Virtual key causing abort
fAbortLeftMouse As Long '// Abort on left mouse?
fAbortRightMouse As Long '// Abort on right mouse?
fLimitEnabled As Long '// Use wTimeLimit?
wTimeLimit As Long '// Seconds to capture
'
fMCIControl As Long '// Use MCI video source?
'
fStepMCIDevice As Long '// Step MCI device?
'
dwMCIStartTime As Long '// Time to start in MS
'
dwMCIStopTime As Long '// Time to stop in MS
'
fStepCaptureAt2x As Long '// Perform spatial averaging 2x
'
wStepCaptureAverageFrames As Long '// Temporal average n Frames
'
dwAudioBufferSize As Long '// Size of audio bufs (0 = default)
'
fDisableWriteCache As Long '// Attempt to disable write cache
AVStreamMaster As Long '// Which stream controls length?
End Type


Type CAPINFOCHUNK
fccInfoID As Long '// Chunk ID, "ICOP" for copyright
lpData As Long '// pointer to data
cbData As Long '// size of lpData
End Type

Type VIDEOHDR
lpData As Long '// address of video buffer
dwBufferLength As Long '// size, in bytes, of the Data buffer
dwBytesUsed As Long '// see below
dwTimeCaptured As Long '// see below
dwUser As Long '// user-specific data
dwFlags As Long '// see below
dwReserved(3) As Long '// reserved; do not use
End Type


'//BITMAP DEFINES (from mmsystem.h)
Public Type BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type

Public Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors() As Long 'array of RGBQUADs
End Type
'***********************************************
'TFrameBufferInfo based on Win32 VIDEOHDR STRUCT
'By Ray Mercer Copyright (c) 1997
'***********************************************
'/* video data block header */
'typedef struct videohdr_tag {
' LPBYTE lpData; /* pointer to locked data buffer */
' DWORD dwBufferLength; /* Length of data buffer */
' DWORD dwBytesUsed; /* Bytes actually used */
' DWORD dwTimeCaptured; /* Milliseconds from start of stream */
' DWORD dwUser; /* for client's use */
' DWORD dwFlags; /* assorted flags (see defines) */
' DWORD dwReserved[4]; /* reserved for driver */
'} VIDEOHDR, NEAR *PVIDEOHDR, FAR * LPVIDEOHDR;
Public Type TFrameBufferInfo
lpData As Long 'locked pointer to frame buffer bits
dwBufLen As Long
dwBytesUsed As Long ' size of locked pointer in bytes
dwTimeStamp As Long
dwUser As Long
dwFlags As Long
dwReserved(4) As Long
End Type
'// Capture Function Declares
Declare Function capCreateCaptureWindow Lib "avicap32.dll" Alias "capCreateCaptureWindowA" _
(ByVal lpszWindowName As String, _
ByVal dwStyle As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal hWndParent As Long, _
ByVal nID As Long) As Long 'returns HWND

Declare Function capGetDriverDescription Lib "avicap32.dll" Alias "capGetDriverDescriptionA" _
(ByVal dwDriverIndex As Long, _
ByVal lpszName As String, _
ByVal cbName As Long, _
ByVal lpszVer As String, _
ByVal cbVer As Long) As Long 'returns C BOOL

'//General WINAPI Declares
Private Declare Function SendMessageAsLong Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Declare Function SendMessageAsAny Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
Private Declare Function SendMessageAsString Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As String) As Long
'// ------------------------------------------------------------------
'// IDs for status and error callbacks
'// ------------------------------------------------------------------

Public Const IDS_CAP_BEGIN As Long = 300 '/* "Capture Start" */
Public Const IDS_CAP_END As Long = 301 '/* "Capture End" */

Public Const IDS_CAP_INFO As Long = 401 '/* "%s" */
Public Const IDS_CAP_OUTOFMEM As Long = 402 '/* "Out of memory" */
Public Const IDS_CAP_FILEEXISTS As Long = 403 '/* "File '%s' exists -- overwrite it?" */
Public Const IDS_CAP_ERRORPALOPEN As Long = 404 '/* "Error opening palette '%s'" */
Public Const IDS_CAP_ERRORPALSAVE As Long = 405 '/* "Error saving palette '%s'" */
Public Const IDS_CAP_ERRORDIBSAVE As Long = 406 '/* "Error saving frame '%s'" */
Public Const IDS_CAP_DEFAVIEXT As Long = 407 '/* "avi" */
Public Const IDS_CAP_DEFPALEXT As Long = 408 '/* "pal" */
Public Const IDS_CAP_CANTOPEN As Long = 409 '/* "Cannot open '%s'" */
Public Const IDS_CAP_SEQ_MSGSTART As Long = 410 '/* "Select OK to start capture\nof video sequence\nto %s." */
Public Const IDS_CAP_SEQ_MSGSTOP As Long = 411 '/* "Hit ESCAPE or click to end capture" */

Public Const IDS_CAP_VIDEDITERR As Long = 412 '/* "An error occurred while trying to run VidEdit." */
Public Const IDS_CAP_READONLYFILE As Long = 413 '/* "The file '%s' is a read-only file." */
Public Const IDS_CAP_WRITEERROR As Long = 414 '/* "Unable to write to file '%s'.\nDisk may be full." */
Public Const IDS_CAP_NODISKSPACE As Long = 415 '/* "There is no space to create a capture file on the specified device." */
Public Const IDS_CAP_SETFILESIZE As Long = 416 '/* "Set File Size" */
Public Const IDS_CAP_SAVEASPERCENT As Long = 417 '/* "SaveAs: %2ld%% Hit Escape to abort." */


资料四
C#捕捉视频头(源码)

using   System;
using   System.Runtime.InteropServices;

namespace   Wuyin.ShoesManager
{
///   
///   VedioCapture   的摘要说明。
///   

public   class   VedioCapture
{
private   int   hCaptureM;
private   bool   isUnLoad   =   false;
public   VedioCapture()
{
}
[DllImport("avicap32.dll")]
private   static   extern   int   capCreateCaptureWindow(   string   strWindowName,   int   dwStyle,   int   x,   int   y   ,int   width,   int   height   ,   int   hwdParent,   int   nID   );
[DllImport("user32.dll")]
private   static   extern   int   SendMessage(   int   hwnd   ,   int   wMsg,   int   wParam   ,   int   lParam   );
[DllImport("user32.dll")]
private   static   extern   int   SendMessage(   int   hwnd   ,   int   wMsg,   int   wParam   ,   string   lParam   );
[DllImport("Kernel32.dll")]
private   static   extern   bool   CloseHandle(   int   hObject   );
public   bool   Initialize(   System.Windows.Forms.Control   aContainer   ,   int   intWidth,   int   intHeight   )
{
hCaptureM   =   capCreateCaptureWindow(   "",   0x40000000   |   0x10000000,   0,0,intWidth,intHeight,aContainer.Handle.ToInt32()   ,1   );
if(   hCaptureM   ==   0   )   return   false;

int   ret   =   SendMessage(   hCaptureM   ,   1034,   0,0   );
if(   ret   ==   0   )
{
CloseHandle(hCaptureM);
return   false;
}
//WM_CAP_SET_PREVIEW
ret   =   SendMessage(   hCaptureM,   1074,   1,   0   );
if(   ret   ==   0   )
{
this.UnLoad();
return   false;
}
//WM_CAP_SET_SCALE
ret   =   SendMessage(   hCaptureM,   1077,   1,   0   );
if(   ret   ==   0   )
{
this.UnLoad();
return   false;
}
//WM_CAP_SET_PREVIEWRATE
ret   =   SendMessage(   hCaptureM,   1076,   66,   0   );
if(   ret   ==   0   )
{
this.UnLoad();
return   false;
}
return   true;
}

public   void   SingleFrameBegin()
{
//
int   ret   =   SendMessage(   hCaptureM,   1094   ,   0,   0   );
}
public   void   SingleFrameEnd()
{
//
int   ret   =   SendMessage(   hCaptureM,   1095   ,   0,   0   );
}

public   void   SingleFrameMode()
{
//WM_CAP_GRAB_FRAME
int   ret   =   SendMessage(     hCaptureM,   1084   ,   0,   0   );
//WM_CAP_SET_PREVIEW
//int   ret   =   SendMessage(     hCaptureM,   1074   ,   0,   0   );
//WM_CAP_SINGLE_FRAME
//ret   =   SendMessage(   hCaptureM,   1096   ,   0,   0   );
}
public   void   PreviewMode()
{
int   ret   =   SendMessage(   hCaptureM,   1074   ,   1,   0   );
}

public   void   UnLoad()
{
int   ret   =   SendMessage(   hCaptureM,   1035,   0,   0   );
CloseHandle(   this.hCaptureM   );
isUnLoad   =   true;
}

public   void   CopyToClipBorad()
{
int   ret   =   SendMessage(     hCaptureM,   1054,   0,   0   );
}

public   void   ShowFormatDialog()
{
int   ret   =   SendMessage(     hCaptureM,   1065,   0,   0   );
}
public   void   SaveToDIB(   string   fileName   )
{
int   ret   =   SendMessage(     hCaptureM,   1049,   0,   fileName   );
}

public   void   ShowDisplayDialog()
{
int   ret   =   SendMessage(   hCaptureM,   1067,   0,   0   );
}
public   System.Drawing.Image   getCaptureImage()
{
System.Windows.Forms.IDataObject   iData   =   System.Windows.Forms.Clipboard.GetDataObject();
System.Drawing.Image   retImage   =   null;
if(   iData   !=     null   )
{
if(   iData.GetDataPresent(   System.Windows.Forms.DataFormats.Bitmap   )   )
{
retImage   =   (System.Drawing.Image)iData.GetData(   System.Windows.Forms.DataFormats.Bitmap   );
}
else   if(   iData.GetDataPresent(   System.Windows.Forms.DataFormats.Dib     )   )
{
retImage   =   (System.Drawing.Image)iData.GetData(   System.Windows.Forms.DataFormats.Dib   );
}
}
return   retImage;
}

~VedioCapture()
{
if(   !isUnLoad   )
{
this.UnLoad();
}
}
}
}

资料五
Imports System.Runtime.InteropServices
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        \'This call is required by the Windows Form Designer.
        InitializeComponent()
        \'Add any initialization after the InitializeComponent() call
    End Sub
    \'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    \'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    \'NOTE: The following procedure is required by the Windows Form Designer
    \'It can be modified using the Windows Form Designer.  
    \'Do not modify it using the code editor.
    Friend WithEvents picCapture As System.Windows.Forms.PictureBox
    Friend WithEvents lstDevices As System.Windows.Forms.ListBox
    Friend WithEvents lblDevice As System.Windows.Forms.Label
    Friend WithEvents btnStart As System.Windows.Forms.Button
    Friend WithEvents btnSave As System.Windows.Forms.Button
    Friend WithEvents btnStop As System.Windows.Forms.Button
    Friend WithEvents sfdImage As System.Windows.Forms.SaveFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.picCapture = New System.Windows.Forms.PictureBox()
        Me.lstDevices = New System.Windows.Forms.ListBox()
        Me.lblDevice = New System.Windows.Forms.Label()
        Me.btnStart = New System.Windows.Forms.Button()
        Me.btnSave = New System.Windows.Forms.Button()
        Me.btnStop = New System.Windows.Forms.Button()
        Me.sfdImage = New System.Windows.Forms.SaveFileDialog()
        Me.SuspendLayout()
        \'
        \'picCapture
        \'
        Me.picCapture.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.picCapture.Location = New System.Drawing.Point(208, 24)
        Me.picCapture.Name = "picCapture"
        Me.picCapture.Size = New System.Drawing.Size(256, 272)
        Me.picCapture.TabIndex = 0
        Me.picCapture.TabStop = False
        \'
        \'lstDevices
        \'
        Me.lstDevices.Location = New System.Drawing.Point(8, 55)
        Me.lstDevices.Name = "lstDevices"
        Me.lstDevices.Size = New System.Drawing.Size(184, 238)
        Me.lstDevices.TabIndex = 1
        \'
        \'lblDevice
        \'
        Me.lblDevice.Location = New System.Drawing.Point(8, 32)
        Me.lblDevice.Name = "lblDevice"
        Me.lblDevice.Size = New System.Drawing.Size(184, 16)
        Me.lblDevice.TabIndex = 2
        Me.lblDevice.Text = "Available Devices"
        Me.lblDevice.TextAlign = System.Drawing.ContentAlignment.TopCenter
        \'
        \'btnStart
        \'
        Me.btnStart.Location = New System.Drawing.Point(20, 320)
        Me.btnStart.Name = "btnStart"
        Me.btnStart.Size = New System.Drawing.Size(112, 32)
        Me.btnStart.TabIndex = 3
        Me.btnStart.Text = "Start Preview"
        \'
        \'btnSave
        \'
        Me.btnSave.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or 
System.Windows.Forms.AnchorStyles.Right)
        Me.btnSave.Location = New System.Drawing.Point(348, 320)
        Me.btnSave.Name = "btnSave"
        Me.btnSave.Size = New System.Drawing.Size(112, 32)
        Me.btnSave.TabIndex = 4
        Me.btnSave.Text = "Save Image"
        \'
        \'btnStop
        \'
        Me.btnStop.Location = New System.Drawing.Point(184, 320)
        Me.btnStop.Name = "btnStop"
        Me.btnStop.Size = New System.Drawing.Size(112, 32)
        Me.btnStop.TabIndex = 5
        Me.btnStop.Text = "Stop Preview"
        \'
        \'sfdImage
        \'
        Me.sfdImage.FileName = "Webcam1"
        Me.sfdImage.Filter = "Bitmap|*.bmp"
        \'
        \'Form1
        \'
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(480, 382)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnStop, Me.btnSave, 
Me.btnStart, Me.lblDevice, Me.lstDevices, Me.picCapture})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Video Capture"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Const WM_CAP As Short = &H400S
    Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
    Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
    Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
    Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
    Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
    Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
    Const WS_CHILD As Integer = &H40000000
    Const WS_VISIBLE As Integer = &H10000000
    Const SWP_NOMOVE As Short = &H2S
    Const SWP_NOSIZE As Short = 1
    Const SWP_NOZORDER As Short = &H4S
    Const HWND_BOTTOM As Short = 1
    Dim iDevice As Integer = 0 \' Current device ID
    Dim hHwnd As Integer \' Handle to preview window
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
        <MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer
    Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, 
_
        ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
        ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
    Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
    Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
        (ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
        ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
        ByVal nHeight As Short, ByVal hWndParent As Integer, _
        ByVal nID As Integer) As Integer
    Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, _
        ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
        ByVal cbVer As Integer) As Boolean
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
Handles MyBase.Load
        LoadDeviceList()
        If lstDevices.Items.Count > 0 Then
            btnStart.Enabled = True
            lstDevices.SelectedIndex = 0
            btnStart.Enabled = True
        Else
            lstDevices.Items.Add("No Capture Device")
            btnStart.Enabled = False
        End If
        btnStop.Enabled = False
        btnSave.Enabled = False
        picCapture.SizeMode = PictureBoxSizeMode.StretchImage
    End Sub
    Private Sub LoadDeviceList()
        Dim strName As String = Space(100)
        Dim strVer As String = Space(100)
        Dim bReturn As Boolean
        Dim x As Integer = 0
        \' 
        \' Load name of all avialable devices into the lstDevices
        \'
        Do
            \'
            \'   Get Driver name and version
            \'
            bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
            \'
            \' If there was a device add device name to the list
            \'
            If bReturn Then lstDevices.Items.Add(strName.Trim)
            x += 1
        Loop Until bReturn = False
    End Sub
    Private Sub OpenPreviewWindow()
        Dim iHeight As Integer = picCapture.Height
        Dim iWidth As Integer = picCapture.Width
        \'
        \' Open Preview window in picturebox
        \'
        hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, _
            480, picCapture.Handle.ToInt32, 0)
        \'
        \' Connect to device
        \'
        If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
            \'
            \'Set the preview scale
            \'
            SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 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, True, 0)
            \'
            \' Resize window to fit in picturebox
            \'
            SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
                    SWP_NOMOVE Or SWP_NOZORDER)
            btnSave.Enabled = True
            btnStop.Enabled = True
            btnStart.Enabled = False
        Else
            \'
            \' Error connecting to device close window
            \' 
            DestroyWindow(hHwnd)
            btnSave.Enabled = False
        End If
    End Sub
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
Handles btnStart.Click
        iDevice = lstDevices.SelectedIndex
        OpenPreviewWindow()
    End Sub
    Private Sub ClosePreviewWindow()
        \'
        \' Disconnect from device
        \'
        SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
        \'
        \' close window
        \'
        DestroyWindow(hHwnd)
    End Sub
    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
Handles btnStop.Click
        ClosePreviewWindow()
        btnSave.Enabled = False
        btnStart.Enabled = True
        btnStop.Enabled = False
    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
Handles btnSave.Click
        Dim data As IDataObject
        Dim bmap As Image
        \'
        \' Copy image to clipboard
        \'
        SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
        \'
        \' Get image from clipboard and convert it to a bitmap
        \'
        data = Clipboard.GetDataObject()
        If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
            bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
            picCapture.Image = bmap
            ClosePreviewWindow()
            btnSave.Enabled = False
            btnStop.Enabled = False
            btnStart.Enabled = True
            If sfdImage.ShowDialog = DialogResult.OK Then
                bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
            End If
        End If
    End Sub
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As 
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If btnStop.Enabled Then
            ClosePreviewWindow()
        End If
    End Sub
End Class