Create a Video from Still Images - Console C#

This tutorial shows how to convert images to video in a C# Windows Console application using the LEADTOOLS SDK.

Summary This tutorial covers how to create a video from multiple images in a C# Windows Console application using the LEADTOOLS Multimedia SDK technology.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (4 KB)
Platform Windows C# Console Application
IDE Visual Studio 2019, 2022
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Create a Video from Still Images - Console C# tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via local DLL reference.

This tutorial requires the following local DLLs, which are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

The following Non-LEADTOOLS DLLs are also required:

For a complete list of which DLLs are required for specific features, refer to Files to be Included in your Application.


Different SDK features require different references. For a complete list, refer to Multimedia Files You Must Include With Your Application.

Set the License File

The License unlocks the features needed. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Setup Resolution, Get Bitmap Size, and Setup Sample Time

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. In the Program class, add the following statements to the using block at the top:

// Using block at the top 
using System; 
using System.IO; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.Multimedia; 

Add three new methods called SetResolution(), GetBitmapSize(Bitmap bmp), and SetSampleTime(MediaSample ms, long frameNo, double AvgTimePerFrame). These methods will be called later when the convert images to video code is added. Add the following code to set up resolution, get bitmap size, and set up sample time.

static void SetResolution(RasterImage image) 
    if (image.BitsPerPixel != 24) 
        ColorResolutionCommand cmd = new ColorResolutionCommand(); 
        cmd.BitsPerPixel = 24; 
        cmd.Mode = ColorResolutionCommandMode.InPlace; 
static int GetBitmapSize(Bitmap bmp) 
    int BytesPerLine = ((bmp.Width * 24 + 31) & ~31) / 8; 
    return BytesPerLine * bmp.Height; 
static void SetSampleTime(MediaSample ms, long frameNo, double AvgTimePerFrame) 
    double timeStart; 
    double timeEnd; 
    timeStart = frameNo * AvgTimePerFrame; 
    timeEnd = (frameNo + 1) * AvgTimePerFrame; 
    // Calculate the high and low part of timeStart 
    ms.SetTime((long)timeStart, (long)timeEnd); 

Convert Still Images to Video

In Program.cs add a new method called Generate and call it in the Main method after SetLicense(); method call. Add the following code to convert the images in a given file to video.

static void Generate() 
    // Create sample source object 
    SampleSource smpsrc = new SampleSource(); 
    ConvertCtrl convertCtrl = new ConvertCtrl(true); 
    // Create a new media type wrapper 
    MediaType mt = new MediaType(); 
    string sourceDirectory = @"C:\LEADTOOLS22\Resources\Images"; 
    string outputFile = @"C:\LEADTOOLS22\Resources\Images\Video-Result.avi"; 
    double fps = 5.0; //frames per second 
    Bitmap bmp = new Bitmap(800, 600, PixelFormat.Format24bppRgb); 
    double AvgTimePerFrame = (10000000 / fps); 
    string[] files = Directory.GetFiles(sourceDirectory, "*.jpg"); 
    // Set the type to 24-bit RGB video 
    mt.Type = Constants.MEDIATYPE_Video; 
    mt.SubType = Constants.MEDIASUBTYPE_RGB24; 
    // Set the format 
    mt.FormatType = Constants.FORMAT_VideoInfo; 
    VideoInfoHeader vih = new VideoInfoHeader(); 
    int bmpSize = GetBitmapSize(bmp); 
    // Setup the video info header 
    vih.bmiHeader.biCompression = 0; // BI_RGB 
    vih.bmiHeader.biBitCount = 24; 
    vih.bmiHeader.biWidth = 800; 
    vih.bmiHeader.biHeight = 600; 
    vih.bmiHeader.biPlanes = 1; 
    vih.bmiHeader.biSizeImage = bmpSize; 
    vih.bmiHeader.biClrImportant = 0; 
    vih.AvgTimePerFrame.lowpart = (int)AvgTimePerFrame; 
    vih.dwBitRate = (int)(bmpSize * 8 * fps); 
    mt.SetVideoFormatData(vih, null, 0); 
    // Set fixed size samples matching the bitmap size 
    mt.SampleSize = bmpSize; 
    mt.FixedSizeSamples = true; 
    // Assign the source media type 
    // Do NOT set a compressor convertCtrl.VideoCompressors.MCmpMJpeg.Selected = true; 
    // Assign the converter source 
    convertCtrl.SourceObject = smpsrc; 
    // Set the output file name 
    convertCtrl.TargetFile = outputFile; 
    convertCtrl.TargetFormat = TargetFormatType.AVI; 
    int i = 0; 
    BitmapData bmpData; 
    byte[] a = new byte[bmpSize]; 
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); 
    using (RasterCodecs codecs = new RasterCodecs()) 
        foreach (string pageFileName in files) 
            using (RasterImage image = codecs.Load(pageFileName)) 
                // Re-size the loaded image to the size of the Bitmap 
                SizeCommand sizecmd = new SizeCommand(); 
                sizecmd.Width = bmp.Width; 
                sizecmd.Height = bmp.Height; 
                ImageIncompatibleReason reason = RasterImageConverter.TestCompatible(image, true); 
                PixelFormat pf = RasterImageConverter.GetNearestPixelFormat(image); 
                if (reason != ImageIncompatibleReason.Compatible) 
                    RasterImageConverter.MakeCompatible(image, pf, true); 
                bmp = (Bitmap)RasterImageConverter.ChangeToImage(image, ChangeToImageOptions.ForceChange); 
                // Bitmaps are bottom left 
                MediaSample ms = smpsrc.GetSampleBuffer(30000); 
                ms.SyncPoint = true; 
                bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); 
                Marshal.Copy(bmpData.Scan0, a, 0, bmpSize); 
                ms.SetData(bmpSize, a); 
                SetSampleTime(ms, i, AvgTimePerFrame); 
                smpsrc.DeliverSample(1000, ms); 
        bmpData = null; 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and gathers all the JPEG files from the <INSTALL_DIR>\LEADTOOLS22\Resources\Images directory and creates an AVI video from the still images.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.