В этом разделе
Позволяет захватывать изображения с устройства захвата изображения.
Объектная модель
Синтаксис
'Declaration
Public NotInheritable Class ImageCaptureSource
public sealed class ImageCaptureSource
public __gc __sealed class ImageCaptureSource
public ref class ImageCaptureSource sealed
Пример
Вот C#/VB.NET код, который демонстрирует, как отображать захваченные изображения с нескольких камер на нескольких формах:
''' <summary>
''' Class that implements image capturing from camera and previewing of captured images in image viewer.
''' </summary>
Public Class VideoPreview
''' <summary>
''' Image viewer where captured images must be shown.
''' </summary>
Private _viewer As Vintasoft.Imaging.UI.ImageViewer
''' <summary>
''' Image capture source.
''' </summary>
Private _captureSource As Vintasoft.Imaging.Media.ImageCaptureSource
Public Sub New(viewer As Vintasoft.Imaging.UI.ImageViewer, captureDevice As Vintasoft.Imaging.Media.ImageCaptureDevice)
_viewer = viewer
_captureSource = New Vintasoft.Imaging.Media.ImageCaptureSource()
_captureSource.CaptureDevice = captureDevice
AddHandler _captureSource.CaptureCompleted, New System.EventHandler(Of Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs)(AddressOf CaptureSource_CaptureCompleted)
End Sub
Public Sub Start()
_captureSource.Start()
_captureSource.CaptureAsync()
End Sub
Public Sub [Stop]()
_captureSource.[Stop]()
End Sub
Private Sub CaptureSource_CaptureCompleted(sender As Object, e As Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs)
Dim oldImage As Vintasoft.Imaging.VintasoftImage = _viewer.Image
_viewer.Image = e.GetCapturedImage()
If oldImage IsNot Nothing Then
oldImage.Dispose()
End If
If _captureSource.State = Vintasoft.Imaging.Media.ImageCaptureState.Started Then
_captureSource.CaptureAsync()
End If
End Sub
End Class
''' <summary>
''' Form for previewing video (captured images) from camera.
''' </summary>
Public Class VideocamForm
Inherits System.Windows.Forms.Form
Private _cameraPreview As VideoPreview
Public Sub New(device As Vintasoft.Imaging.Media.ImageCaptureDevice)
' create image viewer
Dim viewer As New Vintasoft.Imaging.UI.ImageViewer()
viewer.Anchor = System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right Or System.Windows.Forms.AnchorStyles.Top
viewer.Size = New System.Drawing.Size(device.DesiredFormat.Width, device.DesiredFormat.Height)
viewer.SizeMode = Vintasoft.Imaging.UI.ImageSizeMode.BestFit
' set form size and caption
ClientSize = viewer.Size
Text = device.FriendlyName
' add viewer to form
Controls.Add(viewer)
' create video preview
_cameraPreview = New VideoPreview(viewer, device)
End Sub
Protected Overrides Sub OnShown(e As System.EventArgs)
MyBase.OnShown(e)
' start image capturing
_cameraPreview.Start()
End Sub
Protected Overrides Sub OnClosing(e As System.ComponentModel.CancelEventArgs)
' stop image capturing
_cameraPreview.[Stop]()
MyBase.OnClosing(e)
End Sub
End Class
''' <summary>
''' Test for previewing video from all available cameras.
''' </summary>
Public Class VideoPreviewTest
Public Shared Sub ShowVideoFromAllCameras()
' 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
' set device desired formats
For Each device As Vintasoft.Imaging.Media.ImageCaptureDevice In availableDevices
device.DesiredFormat = device.SupportedFormats(0)
Next
' preview video from all available cameras
For Each device As Vintasoft.Imaging.Media.ImageCaptureDevice In availableDevices
' preview video from camera in the first form
Dim myForm As New VideocamForm(device)
myForm.Show()
' preview video from camera in the second form
Dim myForm2 As New VideocamForm(device)
myForm2.Width = myForm2.Width \ 2
myForm2.Height = myForm2.Height \ 2
myForm2.Show()
Next
' if is console application
'Application.Run();
End Sub
End Class
/// <summary>
/// Class that implements image capturing from camera and previewing of captured images in image viewer.
/// </summary>
public class VideoPreview
{
/// <summary>
/// Image viewer where captured images must be shown.
/// </summary>
Vintasoft.Imaging.UI.ImageViewer _viewer;
/// <summary>
/// Image capture source.
/// </summary>
Vintasoft.Imaging.Media.ImageCaptureSource _captureSource;
public VideoPreview(
Vintasoft.Imaging.UI.ImageViewer viewer,
Vintasoft.Imaging.Media.ImageCaptureDevice captureDevice)
{
_viewer = viewer;
_captureSource = new Vintasoft.Imaging.Media.ImageCaptureSource();
_captureSource.CaptureDevice = captureDevice;
_captureSource.CaptureCompleted +=
new System.EventHandler<Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs>(CaptureSource_CaptureCompleted);
}
public void Start()
{
_captureSource.Start();
_captureSource.CaptureAsync();
}
public void Stop()
{
_captureSource.Stop();
}
private void CaptureSource_CaptureCompleted(object sender, Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs e)
{
Vintasoft.Imaging.VintasoftImage oldImage = _viewer.Image;
_viewer.Image = e.GetCapturedImage();
if (oldImage != null)
oldImage.Dispose();
if (_captureSource.State == Vintasoft.Imaging.Media.ImageCaptureState.Started)
_captureSource.CaptureAsync();
}
}
/// <summary>
/// Form for previewing video (captured images) from camera.
/// </summary>
public class VideocamForm : System.Windows.Forms.Form
{
VideoPreview _cameraPreview;
public VideocamForm(Vintasoft.Imaging.Media.ImageCaptureDevice device)
{
// create image viewer
Vintasoft.Imaging.UI.ImageViewer viewer = new Vintasoft.Imaging.UI.ImageViewer();
viewer.Anchor =
System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left |
System.Windows.Forms.AnchorStyles.Right | System.Windows.Forms.AnchorStyles.Top;
viewer.Size =
new System.Drawing.Size(device.DesiredFormat.Width, device.DesiredFormat.Height);
viewer.SizeMode = Vintasoft.Imaging.UI.ImageSizeMode.BestFit;
// set form size and caption
ClientSize = viewer.Size;
Text = device.FriendlyName;
// add viewer to form
Controls.Add(viewer);
// create video preview
_cameraPreview = new VideoPreview(viewer, device);
}
protected override void OnShown(System.EventArgs e)
{
base.OnShown(e);
// start image capturing
_cameraPreview.Start();
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
// stop image capturing
_cameraPreview.Stop();
base.OnClosing(e);
}
}
/// <summary>
/// Test for previewing video from all available cameras.
/// </summary>
public class VideoPreviewTest
{
public static void ShowVideoFromAllCameras()
{
// 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.");
// set device desired formats
foreach (Vintasoft.Imaging.Media.ImageCaptureDevice device in availableDevices)
device.DesiredFormat = device.SupportedFormats[0];
// preview video from all available cameras
foreach (Vintasoft.Imaging.Media.ImageCaptureDevice device in availableDevices)
{
// preview video from camera in the first form
VideocamForm myForm = new VideocamForm(device);
myForm.Show();
// preview video from camera in the second form
VideocamForm myForm2 = new VideocamForm(device);
myForm2.Width = myForm2.Width / 2;
myForm2.Height = myForm2.Height / 2;
myForm2.Show();
}
// if is console application
//Application.Run();
}
}
Иерархия наследования
System.Object
 Vintasoft.Imaging.Media.ImageCaptureSource
Требования
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
Смотрите также