CaptureAsync() Method (ImageCaptureSource)
In This Topic
Initializes an asynchronous image capturing from the
CaptureDevice.
Syntax
public void CaptureAsync()
public: void CaptureAsync()
public:
void CaptureAsync()
'Declaration
Public Sub CaptureAsync()
Remarks
This method can be called only after the Start method.
Retrieve the returned image by handling the CaptureCompleted event of this ImageCaptureSource.
Example
This C#/VB.NET code shows how to capture single image from camera:
Class ImageCaptureSource_CaptureAsync
Public Shared Sub Test(outputFilename As String)
Try
Using image As Vintasoft.Imaging.VintasoftImage = CaptureImageFromCamera()
image.Save(outputFilename)
End Using
Catch ex As System.Exception
System.Console.WriteLine(ex.ToString())
End Try
End Sub
Shared _captureCompletedEventArgs As Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs
Shared _captureFaliedEventArgs As Vintasoft.Imaging.ExceptionEventArgs
Private Shared Function CaptureImageFromCamera() As Vintasoft.Imaging.VintasoftImage
_captureCompletedEventArgs = Nothing
_captureFaliedEventArgs = Nothing
' get available capture devices
Dim availableDevices As System.Collections.ObjectModel.ReadOnlyCollection(Of Vintasoft.Imaging.Media.ImageCaptureDevice) = Vintasoft.Imaging.Media.ImageCaptureDeviceConfiguration.GetCaptureDevices()
If availableDevices.Count = 0 Then
Throw New System.InvalidOperationException("No connected devices.")
End If
' create capture source
Dim captureSource As New Vintasoft.Imaging.Media.ImageCaptureSource()
' add handlers to CaptureCompleted and CaptureFailed events
AddHandler captureSource.CaptureCompleted, New System.EventHandler(Of Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs)(AddressOf captureSource_CaptureCompleted)
AddHandler captureSource.CaptureFailed, New System.EventHandler(Of Vintasoft.Imaging.ExceptionEventArgs)(AddressOf captureSource_CaptureFailed)
' set the first camera as capture device of capture source
captureSource.CaptureDevice = availableDevices(0)
' start capturing
captureSource.Start()
' initialite an asynchronous image capturing
captureSource.CaptureAsync()
' wait while capturing request is finished
While _captureCompletedEventArgs Is Nothing AndAlso _captureFaliedEventArgs Is Nothing
System.Threading.Thread.Sleep(1)
End While
' stop capturing
captureSource.[Stop]()
' if capturing is failed
If _captureFaliedEventArgs IsNot Nothing Then
' throws the exception
Throw _captureFaliedEventArgs.Exception
End If
' return captured image
Return _captureCompletedEventArgs.GetCapturedImage()
End Function
Private Shared Sub captureSource_CaptureFailed(sender As Object, e As Vintasoft.Imaging.ExceptionEventArgs)
_captureFaliedEventArgs = e
End Sub
Private Shared Sub captureSource_CaptureCompleted(sender As Object, e As Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs)
_captureCompletedEventArgs = e
End Sub
End Class
class ImageCaptureSource_CaptureAsync
{
public static void Test(string outputFilename)
{
try
{
using (Vintasoft.Imaging.VintasoftImage image = CaptureImageFromCamera())
image.Save(outputFilename);
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.ToString());
}
}
static Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs _captureCompletedEventArgs;
static Vintasoft.Imaging.ExceptionEventArgs _captureFaliedEventArgs;
private static Vintasoft.Imaging.VintasoftImage CaptureImageFromCamera()
{
_captureCompletedEventArgs = null;
_captureFaliedEventArgs = null;
// get available capture devices
System.Collections.ObjectModel.ReadOnlyCollection<Vintasoft.Imaging.Media.ImageCaptureDevice> availableDevices =
Vintasoft.Imaging.Media.ImageCaptureDeviceConfiguration.GetCaptureDevices();
if (availableDevices.Count == 0)
throw new System.InvalidOperationException("No connected devices.");
// create capture source
Vintasoft.Imaging.Media.ImageCaptureSource captureSource =
new Vintasoft.Imaging.Media.ImageCaptureSource();
// add handlers to CaptureCompleted and CaptureFailed events
captureSource.CaptureCompleted +=
new System.EventHandler<Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs>(captureSource_CaptureCompleted);
captureSource.CaptureFailed +=
new System.EventHandler<Vintasoft.Imaging.ExceptionEventArgs>(captureSource_CaptureFailed);
// set the first camera as capture device of capture source
captureSource.CaptureDevice = availableDevices[0];
// start capturing
captureSource.Start();
// initialite an asynchronous image capturing
captureSource.CaptureAsync();
// wait while capturing request is finished
while (_captureCompletedEventArgs == null && _captureFaliedEventArgs == null)
System.Threading.Thread.Sleep(1);
// stop capturing
captureSource.Stop();
// if capturing is failed
if (_captureFaliedEventArgs != null)
// throws the exception
throw _captureFaliedEventArgs.Exception;
// return captured image
return _captureCompletedEventArgs.GetCapturedImage();
}
private static void captureSource_CaptureFailed(object sender, Vintasoft.Imaging.ExceptionEventArgs e)
{
_captureFaliedEventArgs = e;
}
private static void captureSource_CaptureCompleted(object sender, Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs e)
{
_captureCompletedEventArgs = e;
}
}
Requirements
Target Platforms: .NET9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
See Also