<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF VB WebCam" Height="400" Width="600" Loaded="Window_Loaded" Name="mainWindow">
<!-- Design by Pongsakorn Poosankam -->
<Window.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFD7F4E8" Offset="0.916"/>
<GradientStop Color="#FF6CB595" Offset="0.145"/>
</LinearGradientBrush>
</Window.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.11*"/>
<RowDefinition Height="0.442*"/>
<RowDefinition Height="0.067*"/>
<RowDefinition Height="0.088*"/>
<RowDefinition Height="0.293*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.068*"/>
<ColumnDefinition Width="0.274*"/>
<ColumnDefinition Width="0.069*"/>
<ColumnDefinition Width="0.274*"/>
<ColumnDefinition Width="0.315*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="1" Grid.Row="1" BorderThickness="3" CornerRadius="3">
<Border.BorderBrush>
<RadialGradientBrush>
<GradientStop Color="Black" Offset="0.047"/>
<GradientStop Color="#FF00907A" Offset="1"/>
</RadialGradientBrush>
</Border.BorderBrush>
<Image x:Name="imgVideo" Stretch="Fill" />
</Border>
<Border Grid.Column="3" Grid.Row="1" BorderThickness="3" CornerRadius="3">
<Border.BorderBrush>
<RadialGradientBrush>
<GradientStop Color="Black" Offset="0.047"/>
<GradientStop Color="#FF00907A" Offset="1"/>
</RadialGradientBrush>
</Border.BorderBrush>
<Image x:Name="imgCapture" Stretch="Fill"/>
</Border>
<StackPanel Grid.Column="3" Grid.Row="3" Orientation="Horizontal">
<Button x:Name="bntCapture" Content="Capture Image" Click="bntCapture_Click" />
<Button x:Name="bntSaveImage" Content="Save Image" Margin="8,0,0,0" Click="bntSaveImage_Click" />
</StackPanel>
<StackPanel Grid.Column="4" Grid.Row="1" VerticalAlignment="Center">
<Button x:Name="bntResolution" Content="Video Format" Width="120" Click="bntResolution_Click" />
<Button x:Name="bntSetting" Content="Video Source" Width="120" Margin="0,10,0,0" Click="bntSetting_Click" />
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="bntStart" Content="Start" Click="bntStart_Click" />
<Button x:Name="bntStop" Content="Stop" Margin="10,0,0,0" Click="bntStop_Click" />
<Button x:Name="bntContinue" Content="Continue" Margin="10,0,0,0" Click="bntContinue_Click" />
</StackPanel>
</Grid>
</Window>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Design by Pongsakorn Poosankam
Class Window1
Private webcam As WebCam
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
webcam = New WebCam()
webcam.InitializeWebCam(imgVideo)
End Sub
Private Sub bntStart_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
webcam.Start()
End Sub
Private Sub bntStop_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
webcam.Stop()
End Sub
Private Sub bntContinue_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
webcam.Continue()
End Sub
Private Sub bntCapture_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
imgCapture.Source = imgVideo.Source
End Sub
Private Sub bntSaveImage_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Helper.SaveImageCapture(DirectCast(imgCapture.Source, BitmapSource))
End Sub
Private Sub bntResolution_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
webcam.ResolutionSetting()
End Sub
Private Sub bntSetting_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
webcam.AdvanceSetting()
End Sub
End Class
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Imports System
Imports System.IO
Imports System.Linq
Imports System.Text
Imports WebCam_Capture
Imports System.Windows.Controls
Imports System.Collections.Generic
Imports System.Windows.Media.Imaging
'Design by Pongsakorn Poosankam
Class WebCam
Private webcam As WebCamCapture
Private _FrameImage As System.Windows.Controls.Image
Private FrameNumber As Integer = 30
Public Sub InitializeWebCam(ByRef ImageControl As System.Windows.Controls.Image)
webcam = New WebCamCapture()
webcam.FrameNumber = CULng((0))
webcam.TimeToCapture_milliseconds = FrameNumber
AddHandler webcam.ImageCaptured, AddressOf webcam_ImageCaptured
_FrameImage = ImageControl
End Sub
Private Sub webcam_ImageCaptured(ByVal source As Object, ByVal e As WebcamEventArgs)
_FrameImage.Source = Helper.LoadBitmap(DirectCast(e.WebCamImage, System.Drawing.Bitmap))
End Sub
Public Sub Start()
webcam.TimeToCapture_milliseconds = FrameNumber
webcam.Start(0)
End Sub
Public Sub [Stop]()
webcam.[Stop]()
End Sub
Public Sub [Continue]()
' change the capture time frame
webcam.TimeToCapture_milliseconds = FrameNumber
' resume the video capture from the stop
webcam.Start(Me.webcam.FrameNumber)
End Sub
Public Sub ResolutionSetting()
webcam.Config()
End Sub
Public Sub AdvanceSetting()
webcam.Config2()
End Sub
End Class
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Helper class Helper.vb
This class use for convert Bitmap Image from webcam to
BitmapSource Object.
Imports System
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Collections.Generic
Imports System.Windows.Media.Imaging
'Design by Pongsakorn Poosankam
Class Helper
'Block Memory Leak
<System.Runtime.InteropServices.DllImport("gdi32.dll")> _
Public Shared Function DeleteObject(ByVal handle As IntPtr) As Boolean
End Function
Public Shared bs As BitmapSource
Public Shared ip As IntPtr
Public Shared Function LoadBitmap(ByVal source As System.Drawing.Bitmap) As BitmapSource
ip = source.GetHbitmap()
bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions())
DeleteObject(ip)
Return bs
End Function
Public Shared Sub SaveImageCapture(ByVal bitmap As BitmapSource)
Dim encoder As New JpegBitmapEncoder()
encoder.Frames.Add(BitmapFrame.Create(bitmap))
encoder.QualityLevel = 100
' Configure save file dialog box
Dim dlg As New Microsoft.Win32.SaveFileDialog()
dlg.FileName = "Image"
' Default file name
dlg.DefaultExt = ".Jpg"
' Default file extension
dlg.Filter = "Image (.jpg)|*.jpg"
' Filter files by extension
' Show save file dialog box
Dim result As Nullable(Of Boolean) = dlg.ShowDialog()
' Process save file dialog box results
If result = True Then
' Save Image
Dim filename As String = dlg.FileName
Dim fstream As New FileStream(filename, FileMode.Create)
encoder.Save(fstream)
fstream.Close()
End If
End Sub
End Class
If you found any problem
please report to me by comment at below (login with gmail account before
comment).
Thank you for
try.