How to start animation in WPF image viewer?
In This Topic
A
WpfImageViewer can show animation, which is based on image collection. An image collection can be stored in
WpfImageViewer or separately.
If images are loaded in image viewer and you need to animate images in image viewer, you need to change animation frame using the
WpfImageViewer.SetFocusedIndexSync method.
Here is C#/VB.NET code that shows how to start animation in
WpfImageViewer if images are loaded in image viewer:
/// <summary>
/// Starts showing animation in image viewer.
/// </summary>
/// <param name="viewer">An image viewer.</param>
/// <param name="delay">The animation delay in milliseconds.</param>
private void StartAnimation(Vintasoft.Imaging.Wpf.UI.WpfImageViewer viewer, int delay)
{
// start animation
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(ShowAnimation), new object[] { viewer, delay });
}
/// <summary>
/// Shows animation in image viewer.
/// </summary>
/// <param name="state">Data that the method uses.</param>
private void ShowAnimation(object state)
{
// get data
object[] data = (object[])state;
// get image viewer
Vintasoft.Imaging.Wpf.UI.WpfImageViewer viewer =
(Vintasoft.Imaging.Wpf.UI.WpfImageViewer)data[0];
// get delay value
int delay = (int)data[1];
// 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>
''' Starts showing animation in image viewer.
''' </summary>
''' <param name="viewer">An image viewer.</param>
''' <param name="delay">The animation delay in milliseconds.</param>
Private Sub StartAnimation(viewer As Vintasoft.Imaging.Wpf.UI.WpfImageViewer, delay As Integer)
' start animation
System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf ShowAnimation), New Object() {viewer, delay})
End Sub
''' <summary>
''' Shows animation in image viewer.
''' </summary>
''' <param name="state">Data that the method uses.</param>
Private Sub ShowAnimation(state As Object)
' get data
Dim data As Object() = DirectCast(state, Object())
' get image viewer
Dim viewer As Vintasoft.Imaging.Wpf.UI.WpfImageViewer = DirectCast(data(0), Vintasoft.Imaging.Wpf.UI.WpfImageViewer)
' get delay value
Dim delay As Integer = CInt(data(1))
' 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
If images are stored separately from image viewer and you need to animate images, you need to change animation frame using the
WpfImageViewer.Image property.
Here is C#/VB.NET code that shows how to capture images from camera and show captured images in in
WpfImageViewer:
/// <summary>
/// Image capture source.
/// </summary>
Vintasoft.Imaging.Media.ImageCaptureSource _imageCaptureSource;
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow"/> class.
/// </summary>
public MainWindow()
{
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];
}
Loaded += new System.Windows.RoutedEventHandler(MainWindow_Loaded);
Closed += new System.EventHandler(MainWindow_Closed);
}
/// <summary>
/// Starts capturing from camera.
/// </summary>
private void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs 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 = wpfImageViewer1.Image;
// get captured image and show captured image in the image viewer
wpfImageViewer1.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 MainWindow_Closed(object sender, System.EventArgs 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="MainWindow"/> 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 Loaded, New System.Windows.RoutedEventHandler(AddressOf MainWindow_Loaded)
AddHandler Closed, New System.EventHandler(AddressOf MainWindow_Closed)
End Sub
''' <summary>
''' Starts capturing from camera.
''' </summary>
Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs)
' 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 = wpfImageViewer1.Image
' get captured image and show captured image in the image viewer
wpfImageViewer1.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 MainWindow_Closed(sender As Object, e As System.EventArgs)
' stop the image capture source
_imageCaptureSource.[Stop]()
End Sub
Also, you can use
WpfAnimatedImageViewer to show animation.
Here is C#/VB.NET code that shows how to show animation from GIF image file in
WpfAnimatedImageViewer:
/// <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.Wpf.UI.WpfAnimatedImageViewer 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.Wpf.UI.WpfAnimatedImageViewer, 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