VintaSoft Imaging .NET SDK 15.1: Документация для .NET разработчика
Vintasoft.Imaging.Media Namespace / ImageCaptureSource Class / CaptureAsync() Method
Синтаксис Ремарки Example Требования Смотрите также
В этом разделе
CaptureAsync() Метод (ImageCaptureSource)
В этом разделе
Инициализирует асинхронный захват изображения из CaptureDevice.
Синтаксис
'Declaration

Public Sub CaptureAsync()

public void CaptureAsync()

Ремарки

Этот метод может быть вызван только после метода Start.

Извлеките возвращенное изображение, обработав событие CaptureCompleted этого ImageCaptureSource.

Пример

Вот C#/VB.NET код, который демонстрирует, как захватить одно изображение с камеры:


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;
    }

}

Требования

Целевые платформы: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

Смотрите также