Aurigma Graphics Mill 5.5 for .NET
Creating Thumbnails for Video
One of the typical tasks where Media Processor Add-on can be useful is creating video thumbnails. For example, if you want to create a collection of your video files, for each of them you can store a small preview image along with other descriptive data. Later these screenshots can be displayed while you browse the collection.
The function that will create thumbnails should perform the following operations:
- Create a reader and open the source file.
- Find a non-empty frame in the source file. The simplest way to do it is to read a frame from the middle of the video file using the IFormatReader.LoadFrame(Int32) method.
- Create an empty instance of the Bitmap object.
- Initialize the Bitmap object with the
resized frame in one of the ways:
- Call the GetThumbnail(Bitmap, Int32, Int32) method to get a resized version of the frame.
- Call the GetBitmap(Bitmap) method to get the frame as it is. Then, if additional transformations are required, modify the Bitmap object.
- Save the bitmap, for example, with the help of the Save method.
- Close the reader.
The following code snippets show how this can be done.
Visual Basic
Public Sub CreateVideoThumbnail(ByVal inputFilename As String, ByVal _
outputBaseFilename As String)
Try
'Create the reader
Dim reader As IFormatReader = MediaFormatManager.CreateFormatReader _
(inputFilename)
'Get the frame
If reader.FrameCount < 1 Then
Console.WriteLine("File contains no frames.")
Return
End If
Dim index As Int32 = reader.FrameCount / 2
Dim frame As IFrame = reader.LoadFrame(index)
'Save the frame using GetThumbnail()
Dim image1 As Aurigma.GraphicsMill.Bitmap = New _
Aurigma.GraphicsMill.Bitmap
frame.GetThumbnail(image1, 160, 120)
image1.Save(outputBaseFilename + "1.gif")
image1.Dispose()
'Save the frame using GetBitmap() and Resize
Dim image2 As Aurigma.GraphicsMill.Bitmap = New _
Aurigma.GraphicsMill.Bitmap
frame.GetBitmap(image2)
Dim transformation As Resize = New Resize(160, 120)
transformation.ApplyTransform(image2)
image2.Save(outputBaseFilename + "2.gif")
image2.Dispose()
reader.Dispose()
Catch ex As Exception
Console.WriteLine(ex)
End Try
End Sub
C#
public static void CreateVideoThumbnail(string inputFilename,
string outputBaseFilename)
{
try
{
//Create the reader
IFormatReader reader = MediaFormatManager.CreateFormatReader(inputFilename);
//Get the frame
if (reader.FrameCount < 1)
{
Console.WriteLine("File contains no frames.");
return;
}
Int32 index = reader.FrameCount / 2;
IFrame frame = reader.LoadFrame(index);
//Save the frame using GetThumbnail()
Aurigma.GraphicsMill.Bitmap image1 = new Aurigma.GraphicsMill.Bitmap();
frame.GetThumbnail(image1, 160, 120);
image1.Save(outputBaseFilename + @"1.gif");
image1.Dispose();
//Save the frame using GetBitmap() and Resize
Aurigma.GraphicsMill.Bitmap image2 = new Aurigma.GraphicsMill.Bitmap();
frame.GetBitmap(image2);
Resize transformation = new Resize(160, 120);
transformation.ApplyTransform(image2);
image2.Save(outputBaseFilename + @"2.gif");
image2.Dispose();
reader.Dispose();
}
catch (System.Exception ex)
{
Console.WriteLine(ex);
}
}