Advertisement
Guest User

Kev

a guest
Oct 24th, 2008
1,933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 10.72 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Form1
  4.     Inherits System.Windows.Forms.Form
  5.  
  6. #Region " Windows Form Designer generated code "
  7.  
  8.     Public Sub New()
  9.         MyBase.New()
  10.  
  11.         'This call is required by the Windows Form Designer.
  12.         InitializeComponent()
  13.  
  14.         'Add any initialization after the InitializeComponent() call
  15.  
  16.     End Sub
  17.  
  18.     'Form overrides dispose to clean up the component list.
  19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  20.         If disposing Then
  21.             If Not (components Is Nothing) Then
  22.                 components.Dispose()
  23.             End If
  24.         End If
  25.         MyBase.Dispose(disposing)
  26.     End Sub
  27.  
  28.     'Required by the Windows Form Designer
  29.     Private components As System.ComponentModel.IContainer
  30.  
  31.     'NOTE: The following procedure is required by the Windows Form Designer
  32.     'It can be modified using the Windows Form Designer.  
  33.     'Do not modify it using the code editor.
  34.     Friend WithEvents picCapture As System.Windows.Forms.PictureBox
  35.     Friend WithEvents lstDevices As System.Windows.Forms.ListBox
  36.     Friend WithEvents lblDevice As System.Windows.Forms.Label
  37.     Friend WithEvents btnStart As System.Windows.Forms.Button
  38.     Friend WithEvents btnSave As System.Windows.Forms.Button
  39.     Friend WithEvents btnStop As System.Windows.Forms.Button
  40.     Friend WithEvents sfdImage As System.Windows.Forms.SaveFileDialog
  41.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  42.         Me.picCapture = New System.Windows.Forms.PictureBox()
  43.         Me.lstDevices = New System.Windows.Forms.ListBox()
  44.         Me.lblDevice = New System.Windows.Forms.Label()
  45.         Me.btnStart = New System.Windows.Forms.Button()
  46.         Me.btnSave = New System.Windows.Forms.Button()
  47.         Me.btnStop = New System.Windows.Forms.Button()
  48.         Me.sfdImage = New System.Windows.Forms.SaveFileDialog()
  49.         Me.SuspendLayout()
  50.         '
  51.         'picCapture
  52.         '
  53.         Me.picCapture.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  54.         Me.picCapture.Location = New System.Drawing.Point(208, 24)
  55.         Me.picCapture.Name = "picCapture"
  56.         Me.picCapture.Size = New System.Drawing.Size(256, 272)
  57.         Me.picCapture.TabIndex = 0
  58.         Me.picCapture.TabStop = False
  59.         '
  60.         'lstDevices
  61.         '
  62.         Me.lstDevices.Location = New System.Drawing.Point(8, 55)
  63.         Me.lstDevices.Name = "lstDevices"
  64.         Me.lstDevices.Size = New System.Drawing.Size(184, 238)
  65.         Me.lstDevices.TabIndex = 1
  66.         '
  67.         'lblDevice
  68.         '
  69.         Me.lblDevice.Location = New System.Drawing.Point(8, 32)
  70.         Me.lblDevice.Name = "lblDevice"
  71.         Me.lblDevice.Size = New System.Drawing.Size(184, 16)
  72.         Me.lblDevice.TabIndex = 2
  73.         Me.lblDevice.Text = "Available Devices"
  74.         Me.lblDevice.TextAlign = System.Drawing.ContentAlignment.TopCenter
  75.         '
  76.         'btnStart
  77.         '
  78.         Me.btnStart.Location = New System.Drawing.Point(20, 320)
  79.         Me.btnStart.Name = "btnStart"
  80.         Me.btnStart.Size = New System.Drawing.Size(112, 32)
  81.         Me.btnStart.TabIndex = 3
  82.         Me.btnStart.Text = "Start Preview"
  83.         '
  84.         'btnSave
  85.         '
  86.         Me.btnSave.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
  87.         Me.btnSave.Location = New System.Drawing.Point(348, 320)
  88.         Me.btnSave.Name = "btnSave"
  89.         Me.btnSave.Size = New System.Drawing.Size(112, 32)
  90.         Me.btnSave.TabIndex = 4
  91.         Me.btnSave.Text = "Save Image"
  92.         '
  93.         'btnStop
  94.         '
  95.         Me.btnStop.Location = New System.Drawing.Point(184, 320)
  96.         Me.btnStop.Name = "btnStop"
  97.         Me.btnStop.Size = New System.Drawing.Size(112, 32)
  98.         Me.btnStop.TabIndex = 5
  99.         Me.btnStop.Text = "Stop Preview"
  100.         '
  101.         'sfdImage
  102.         '
  103.         Me.sfdImage.FileName = "Webcam1"
  104.         Me.sfdImage.Filter = "Bitmap|*.bmp"
  105.         '
  106.         'Form1
  107.         '
  108.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  109.         Me.ClientSize = New System.Drawing.Size(480, 382)
  110.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnStop, Me.btnSave, Me.btnStart, Me.lblDevice, Me.lstDevices, Me.picCapture})
  111.         Me.Name = "Form1"
  112.         Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  113.         Me.Text = "Video Capture"
  114.         Me.ResumeLayout(False)
  115.  
  116.     End Sub
  117.  
  118. #End Region
  119.  
  120.     Const WM_CAP As Short = &H400S
  121.  
  122.     Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
  123.     Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
  124.     Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
  125.  
  126.     Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
  127.     Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
  128.     Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
  129.     Const WS_CHILD As Integer = &H40000000
  130.     Const WS_VISIBLE As Integer = &H10000000
  131.     Const SWP_NOMOVE As Short = &H2S
  132.     Const SWP_NOSIZE As Short = 1
  133.     Const SWP_NOZORDER As Short = &H4S
  134.     Const HWND_BOTTOM As Short = 1
  135.  
  136.     Dim iDevice As Integer = 0 ' Current device ID
  137.     Dim hHwnd As Integer ' Handle to preview window
  138.  
  139.     Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  140.         (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
  141.         <MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer
  142.  
  143.     Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, _
  144.         ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
  145.         ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
  146.  
  147.     Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
  148.  
  149.     Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
  150.         (ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
  151.         ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
  152.         ByVal nHeight As Short, ByVal hWndParent As Integer, _
  153.         ByVal nID As Integer) As Integer
  154.  
  155.     Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, _
  156.         ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
  157.         ByVal cbVer As Integer) As Boolean
  158.  
  159.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  160.         LoadDeviceList()
  161.         If lstDevices.Items.Count > 0 Then
  162.             btnStart.Enabled = True
  163.             lstDevices.SelectedIndex = 0
  164.             btnStart.Enabled = True
  165.         Else
  166.             lstDevices.Items.Add("No Capture Device")
  167.             btnStart.Enabled = False
  168.         End If
  169.  
  170.         btnStop.Enabled = False
  171.         btnSave.Enabled = False
  172.         picCapture.SizeMode = PictureBoxSizeMode.StretchImage
  173.     End Sub
  174.  
  175.     Private Sub LoadDeviceList()
  176.         Dim strName As String = Space(100)
  177.         Dim strVer As String = Space(100)
  178.         Dim bReturn As Boolean
  179.         Dim x As Integer = 0
  180.  
  181.         '
  182.         ' Load name of all avialable devices into the lstDevices
  183.         '
  184.  
  185.         Do
  186.             '
  187.             '   Get Driver name and version
  188.             '
  189.             bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
  190.  
  191.             '
  192.             ' If there was a device add device name to the list
  193.             '
  194.             If bReturn Then lstDevices.Items.Add(strName.Trim)
  195.             x += 1
  196.         Loop Until bReturn = False
  197.     End Sub
  198.  
  199.     Private Sub OpenPreviewWindow()
  200.         Dim iHeight As Integer = picCapture.Height
  201.         Dim iWidth As Integer = picCapture.Width
  202.  
  203.         '
  204.         ' Open Preview window in picturebox
  205.         '
  206.         hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, _
  207.             480, picCapture.Handle.ToInt32, 0)
  208.  
  209.         '
  210.         ' Connect to device
  211.         '
  212.         If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
  213.             '
  214.             'Set the preview scale
  215.             '
  216.             SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
  217.  
  218.             '
  219.             'Set the preview rate in milliseconds
  220.             '
  221.             SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
  222.  
  223.             '
  224.             'Start previewing the image from the camera
  225.             '
  226.             SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
  227.  
  228.             '
  229.             ' Resize window to fit in picturebox
  230.             '
  231.             SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
  232.                     SWP_NOMOVE Or SWP_NOZORDER)
  233.  
  234.             btnSave.Enabled = True
  235.             btnStop.Enabled = True
  236.             btnStart.Enabled = False
  237.         Else
  238.             '
  239.             ' Error connecting to device close window
  240.             '
  241.             DestroyWindow(hHwnd)
  242.  
  243.             btnSave.Enabled = False
  244.         End If
  245.     End Sub
  246.  
  247.     Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
  248.         iDevice = lstDevices.SelectedIndex
  249.         OpenPreviewWindow()
  250.     End Sub
  251.  
  252.     Private Sub ClosePreviewWindow()
  253.         '
  254.         ' Disconnect from device
  255.         '
  256.         SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
  257.  
  258.         '
  259.         ' close window
  260.         '
  261.  
  262.         DestroyWindow(hHwnd)
  263.     End Sub
  264.  
  265.     Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
  266.         ClosePreviewWindow()
  267.         btnSave.Enabled = False
  268.         btnStart.Enabled = True
  269.         btnStop.Enabled = False
  270.     End Sub
  271.  
  272.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
  273.         Dim data As IDataObject
  274.         Dim bmap As Image
  275.  
  276.         '
  277.         ' Copy image to clipboard
  278.         '
  279.         SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
  280.  
  281.         '
  282.         ' Get image from clipboard and convert it to a bitmap
  283.         '
  284.         data = Clipboard.GetDataObject()
  285.         If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
  286.             bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
  287.             picCapture.Image = bmap
  288.             ClosePreviewWindow()
  289.             btnSave.Enabled = False
  290.             btnStop.Enabled = False
  291.             btnStart.Enabled = True
  292.  
  293.             If sfdImage.ShowDialog = DialogResult.OK Then
  294.                 bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
  295.             End If
  296.  
  297.         End If
  298.     End Sub
  299.  
  300.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
  301.         If btnStop.Enabled Then
  302.             ClosePreviewWindow()
  303.         End If
  304.     End Sub
  305. End Class
  306.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement