VintaSoft Imaging .NET SDK 15.1: Документация для .NET разработчика
Vintasoft.Imaging.Media Namespace / DirectShowCamera Class
Члены типа Объект Синтаксис Example Иерархия Требования Смотрите также
В этом разделе
Класс DirectShowCamera
В этом разделе
Представляет DirectShow-камеру.
Объектная модель
DirectShowCameraControlProperties DirectShowImageQualityProperties ImageCaptureFormat DirectShowCamera
Синтаксис
'Declaration

Public NotInheritable Class DirectShowCamera
   Inherits ImageCaptureDevice

public sealed class DirectShowCamera : ImageCaptureDevice

Пример

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


Class DirectShowCamera_CaptureImageAsync

    ''' <summary>
    ''' Indicates that image is captured from device.
    ''' </summary>
    Shared _captureCompleted As Boolean



    ''' <summary>
    ''' Captures image from camera and saves captured image to a file.
    ''' </summary>
    Public Shared Sub CaptureAndSaveImage()
        _captureCompleted = False

        ' get a list of available cameras
        Dim captureDevices As System.Collections.ObjectModel.ReadOnlyCollection(Of Vintasoft.Imaging.Media.ImageCaptureDevice) = Vintasoft.Imaging.Media.ImageCaptureDeviceConfiguration.GetCaptureDevices()

        ' if cameras are not found
        If captureDevices.Count = 0 Then
            System.Console.WriteLine("No connected devices.")
            Return
        End If

        ' get the first available camera
        Dim captureDevice As Vintasoft.Imaging.Media.DirectShowCamera = DirectCast(captureDevices(0), Vintasoft.Imaging.Media.DirectShowCamera)

        ' create the image capture source
        Dim captureSource As New Vintasoft.Imaging.Media.ImageCaptureSource()
        ' set the camera as a capture device of the image capture source
        captureSource.CaptureDevice = captureDevice
        ' subscribe to the image cptured event
        AddHandler captureSource.CaptureCompleted, New System.EventHandler(Of Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs)(AddressOf captureSource_CaptureCompleted)

        ' start the image capturing from the camera
        captureSource.Start()

        ' asynchronously capture an image from camera
        captureSource.CaptureAsync()

        ' while image is not captured
        While Not _captureCompleted
            ' wait
            System.Windows.Forms.Application.DoEvents()
        End While

        ' stop the image capturing from the camera
        captureSource.[Stop]()
    End Sub

    ''' <summary>
    ''' Image is captured from camera.
    ''' </summary>
    Private Shared Sub captureSource_CaptureCompleted(sender As Object, e As Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs)
        ' get captured image as VintasoftImage object
        Using capturedImage As Vintasoft.Imaging.VintasoftImage = e.GetCapturedImage()
            ' output information about captured image
            System.Console.WriteLine(String.Format("Image is captured: Width={0}, Height={1}", capturedImage.Width, capturedImage.Height))

            ' save captured image to a file
            capturedImage.Save("capturedImage.png")
            System.Console.WriteLine("Image is saved.")
        End Using

        ' indicate that image is captured
        _captureCompleted = True
    End Sub

End Class


class DirectShowCamera_CaptureImageAsync
{

    /// <summary>
    /// Indicates that image is captured from device.
    /// </summary>
    static bool _captureCompleted;



    /// <summary>
    /// Captures image from camera and saves captured image to a file.
    /// </summary>
    public static void CaptureAndSaveImage()
    {
        _captureCompleted = false;

        // get a list of available cameras
        System.Collections.ObjectModel.ReadOnlyCollection<Vintasoft.Imaging.Media.ImageCaptureDevice> captureDevices =
            Vintasoft.Imaging.Media.ImageCaptureDeviceConfiguration.GetCaptureDevices();

        // if cameras are not found
        if (captureDevices.Count == 0)
        {
            System.Console.WriteLine("No connected devices.");
            return;
        }

        // get the first available camera
        Vintasoft.Imaging.Media.DirectShowCamera captureDevice =
            (Vintasoft.Imaging.Media.DirectShowCamera)captureDevices[0];

        // create the image capture source
        Vintasoft.Imaging.Media.ImageCaptureSource captureSource =
            new Vintasoft.Imaging.Media.ImageCaptureSource();
        // set the camera as a capture device of the image capture source
        captureSource.CaptureDevice = captureDevice;
        // subscribe to the image cptured event
        captureSource.CaptureCompleted +=
            new System.EventHandler<Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs>(captureSource_CaptureCompleted);

        // start the image capturing from the camera
        captureSource.Start();

        // asynchronously capture an image from camera
        captureSource.CaptureAsync();

        // while image is not captured
        while (!_captureCompleted)
            // wait
            System.Windows.Forms.Application.DoEvents();

        // stop the image capturing from the camera
        captureSource.Stop();
    }

    /// <summary>
    /// Image is captured from camera.
    /// </summary>
    private static void captureSource_CaptureCompleted(object sender, Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs e)
    {
        // get captured image as VintasoftImage object
        using (Vintasoft.Imaging.VintasoftImage capturedImage = e.GetCapturedImage())
        {
            // output information about captured image
            System.Console.WriteLine(string.Format("Image is captured: Width={0}, Height={1}", capturedImage.Width, capturedImage.Height));

            // save captured image to a file
            capturedImage.Save("capturedImage.png");
            System.Console.WriteLine("Image is saved.");
        }

        // indicate that image is captured
        _captureCompleted = true;
    }

}

Иерархия наследования

System.Object
   Vintasoft.Imaging.Media.ImageCaptureDevice
      Vintasoft.Imaging.Media.DirectShowCamera

Требования

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

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