Как запустить анимацию в WinForms просмотрщике изображений?
В этом разделе
ImageViewer может отображать анимацию, основанную на коллекции изображений. Коллекция изображений может храниться в
ImageViewer-е или отдельно.
Если изображения загружены в просмотрщик изображений и вам необходимо анимировать изображения в просмотрщике изображений, вам необходимо изменять кадр анимации с помощью метода
ImageViewer.SetFocusedIndexSync.
Вот C#/VB.NET код, который демонстрирует, как запустить анимацию в
ImageViewer, если изображения загружены в просмотрщике изображений:
/// <summary>
/// Shows animation in image viewer.
/// </summary>
/// <param name="viewer">An image viewer.</param>
/// <param name="delay">The animation delay in milliseconds.</param>
private void ShowAnimation(Vintasoft.Imaging.UI.ImageViewer viewer, int delay)
{
// for each image in image collection of image viewer
for (int i = 0; i < viewer.Images.Count; i++)
{
// change image in image viewer
viewer.SetFocusedIndexSync(i);
// sleep for a while
System.Threading.Thread.Sleep(delay);
}
}
''' <summary>
''' Shows animation in image viewer.
''' </summary>
''' <param name="viewer">An image viewer.</param>
''' <param name="delay">The animation delay in milliseconds.</param>
Private Sub ShowAnimation(viewer As Vintasoft.Imaging.UI.ImageViewer, delay As Integer)
' for each image in image collection of image viewer
For i As Integer = 0 To viewer.Images.Count - 1
' change image in image viewer
viewer.SetFocusedIndexSync(i)
' sleep for a while
System.Threading.Thread.Sleep(delay)
Next
End Sub
Если изображения хранятся отдельно от просмотрщика изображений и вам необходимо анимировать изображения, вам необходимо изменить кадр анимации с помощью свойства
ImageViewer.Image.
Вот C#/VB.NET код, который демонстрирует, как захватывать изображения с камеры и отображать захваченные изображения в
ImageViewer:
/// <summary>
/// Image capture source.
/// </summary>
Vintasoft.Imaging.Media.ImageCaptureSource _imageCaptureSource;
/// <summary>
/// Initializes a new instance of the <see cref="MainForm"/> class.
/// </summary>
public MainForm()
{
InitializeComponent();
// get available image capture devices
System.Collections.ObjectModel.ReadOnlyCollection<Vintasoft.Imaging.Media.ImageCaptureDevice> availableDevices =
Vintasoft.Imaging.Media.ImageCaptureDeviceConfiguration.GetCaptureDevices();
// create new image capture source
_imageCaptureSource = new Vintasoft.Imaging.Media.ImageCaptureSource();
_imageCaptureSource.CaptureCompleted +=
new System.EventHandler<Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs>(ImageCaptureSource_CaptureCompleted);
// if there are available devices
if (availableDevices.Count != 0)
{
// use the first image capture device
_imageCaptureSource.CaptureDevice = availableDevices[0];
}
Shown += new System.EventHandler(MainForm_Shown);
FormClosed += new System.Windows.Forms.FormClosedEventHandler(MainForm_FormClosed);
}
/// <summary>
/// Starts capturing from camera.
/// </summary>
private void MainForm_Shown(object sender, System.EventArgs e)
{
// if capture device is empty
if (_imageCaptureSource.CaptureDevice != null)
{
// start the image capture source
_imageCaptureSource.Start();
// initialize new image capture request
_imageCaptureSource.CaptureAsync();
}
}
/// <summary>
/// Image is captured.
/// </summary>
private void ImageCaptureSource_CaptureCompleted(object sender, Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs e)
{
// save reference to the previously captured image
Vintasoft.Imaging.VintasoftImage oldImage = imageViewer1.Image;
// get captured image and show captured image in the image viewer
imageViewer1.Image = e.GetCapturedImage();
// if previously captured image exists
if (oldImage != null)
{
// dispose previously captured image
oldImage.Dispose();
}
// if capture source is started
if (_imageCaptureSource.State == Vintasoft.Imaging.Media.ImageCaptureState.Started)
{
// initialize new image capture request
_imageCaptureSource.CaptureAsync();
}
}
/// <summary>
/// Form is closed.
/// </summary>
private void MainForm_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
{
// stop the image capture source
_imageCaptureSource.Stop();
}
''' <summary>
''' Image capture source.
''' </summary>
Private _imageCaptureSource As Vintasoft.Imaging.Media.ImageCaptureSource
''' <summary>
''' Initializes a new instance of the <see cref="MainForm"/> class.
''' </summary>
Public Sub New()
InitializeComponent()
' get available image capture devices
Dim availableDevices As System.Collections.ObjectModel.ReadOnlyCollection(Of Vintasoft.Imaging.Media.ImageCaptureDevice) = Vintasoft.Imaging.Media.ImageCaptureDeviceConfiguration.GetCaptureDevices()
' create new image capture source
_imageCaptureSource = New Vintasoft.Imaging.Media.ImageCaptureSource()
AddHandler _imageCaptureSource.CaptureCompleted, New System.EventHandler(Of Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs)(AddressOf ImageCaptureSource_CaptureCompleted)
' if there are available devices
If availableDevices.Count <> 0 Then
' use the first image capture device
_imageCaptureSource.CaptureDevice = availableDevices(0)
End If
AddHandler Shown, New System.EventHandler(AddressOf MainForm_Shown)
AddHandler FormClosed, New System.Windows.Forms.FormClosedEventHandler(AddressOf MainForm_FormClosed)
End Sub
''' <summary>
''' Starts capturing from camera.
''' </summary>
Private Sub MainForm_Shown(sender As Object, e As System.EventArgs)
' if capture device is empty
If _imageCaptureSource.CaptureDevice IsNot Nothing Then
' start the image capture source
_imageCaptureSource.Start()
' initialize new image capture request
_imageCaptureSource.CaptureAsync()
End If
End Sub
''' <summary>
''' Image is captured.
''' </summary>
Private Sub ImageCaptureSource_CaptureCompleted(sender As Object, e As Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs)
' save reference to the previously captured image
Dim oldImage As Vintasoft.Imaging.VintasoftImage = imageViewer1.Image
' get captured image and show captured image in the image viewer
imageViewer1.Image = e.GetCapturedImage()
' if previously captured image exists
If oldImage IsNot Nothing Then
' dispose previously captured image
oldImage.Dispose()
End If
' if capture source is started
If _imageCaptureSource.State = Vintasoft.Imaging.Media.ImageCaptureState.Started Then
' initialize new image capture request
_imageCaptureSource.CaptureAsync()
End If
End Sub
''' <summary>
''' Form is closed.
''' </summary>
Private Sub MainForm_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs)
' stop the image capture source
_imageCaptureSource.[Stop]()
End Sub
Кроме того, вы можете использовать
AnimatedImageViewer чтобы показать анимацию.
Вот C#/VB.NET код, который демонстрирует, как отображать анимацию из GIF файла в
AnimatedImageViewer:
/// <summary>
/// Shows animation in animated image viewer.
/// </summary>
/// <param name="viewer">An animated image viewer.</param>
/// <param name="filename">The filename.</param>
private void StartAnimation(Vintasoft.Imaging.UI.AnimatedImageViewer viewer, string filename)
{
// if image collection of the image viewer is not empty
if (viewer.Images.Count > 0)
{
// clear the image collection of the image viewer
viewer.Images.ClearAndDisposeItems();
}
// open the file
viewer.Images.Add(filename);
// start the animation
viewer.Animation = true;
}
''' <summary>
''' Shows animation in animated image viewer.
''' </summary>
''' <param name="viewer">An animated image viewer.</param>
''' <param name="filename">The filename.</param>
Private Sub StartAnimation(viewer As Vintasoft.Imaging.UI.AnimatedImageViewer, filename As String)
' if image collection of the image viewer is not empty
If viewer.Images.Count > 0 Then
' clear the image collection of the image viewer
viewer.Images.ClearAndDisposeItems()
End If
' open the file
viewer.Images.Add(filename)
' start the animation
viewer.Animation = True
End Sub