This tutorial shows how to convert images to a video file in a C# .NET 6 application using the LEADTOOLS Multimedia SDK.
| Overview | |
|---|---|
| Summary | This tutorial covers how to create a video from multiple images in a C# .NET 6 application. |
| Completion Time | 20 minutes |
| Visual Studio Project | Download tutorial project (2 KB) |
| Platform | C# .NET 6 Console Application |
| IDE | Visual Studio 2022 |
| Runtime Target | .NET 6 or Higher |
| Development License | Download LEADTOOLS |
| Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on this tutorial.
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>\LEADTOOLS23\Bin\net:
Leadtools.Codecs.dllLeadtools.Converters.dllLeadtools.Core.dllLeadtools.dllLeadtools.Drawing.Windows.dllLeadtools.Drawing.dllLeadtools.ImageProcessing.Color.dllLeadtools.ImageProcessing.Core.dllLeadtools.Multimedia.dllSystem.Windows.Forms.dllFor a complete list of which DLLs are required for specific features, refer to Files to be Included in your Application and Multimedia Files You Must Include With Your Application.
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:
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 System.Drawing.Imaging;using System.Runtime.InteropServices;using Leadtools;using Leadtools.Codecs;using Leadtools.Drawing;using Leadtools.ImageProcessing;using Leadtools.Multimedia;
Add three new methods named 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 the 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;cmd.Run(image);}}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 timeStartms.SetTime((long)timeStart, (long)timeEnd);}
In Program.cs add a new method named Generate() and call it in the Main method after InitLEAD method call. Add the following code to convert the images in a given file to video.
static void Generate(){// Create sample source objectSampleSource smpsrc = new SampleSource();ConvertCtrl convertCtrl = new ConvertCtrl(true);// Create a new media type wrapperMediaType mt = new MediaType();string sourceDirectory = @"C:\LEADTOOLS23\Resources\Images";string outputFile = @"C:\LEADTOOLS23\Resources\Images\Video-Result.avi";double fps = 5.0; //frames per secondBitmap bmp = new Bitmap(800, 600, PixelFormat.Format24bppRgb);double AvgTimePerFrame = (10000000 / fps);string[] files = Directory.GetFiles(sourceDirectory, "*.jpg");// Set the type to 24-bit RGB videomt.Type = Constants.MEDIATYPE_Video;mt.SubType = Constants.MEDIASUBTYPE_RGB24;// Set the formatmt.FormatType = Constants.FORMAT_VideoInfo;VideoInfoHeader vih = new VideoInfoHeader();int bmpSize = GetBitmapSize(bmp);// Setup the video info headervih.bmiHeader.biCompression = 0; // BI_RGBvih.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 sizemt.SampleSize = bmpSize;mt.FixedSizeSamples = true;// Assign the source media typesmpsrc.SetMediaType(mt);// Do NOT set a compressor convertCtrl.VideoCompressors.MCmpMJpeg.Selected = true;// Assign the converter sourceconvertCtrl.SourceObject = smpsrc;// Set the output file nameconvertCtrl.TargetFile = outputFile;convertCtrl.TargetFormat = TargetFormatType.AVI;convertCtrl.StartConvert();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);SetResolution(image);// Re-size the loaded image to the size of the BitmapSizeCommand sizecmd = new SizeCommand();sizecmd.Width = bmp.Width;sizecmd.Height = bmp.Height;sizecmd.Run(image);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 leftbmp.RotateFlip(RotateFlipType.RotateNoneFlipY);MediaSample ms = smpsrc.GetSampleBuffer(30000);ms.SyncPoint = true;bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);Marshal.Copy(bmpData.Scan0, a, 0, bmpSize);bmp.UnlockBits(bmpData);ms.SetData(bmpSize, a);SetSampleTime(ms, i, AvgTimePerFrame);smpsrc.DeliverSample(1000, ms);i++;}bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);bmpData = null;smpsrc.DeliverEndOfStream(1000);System.Threading.Thread.Sleep(1);MessageBox.Show("Finished");}
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>\LEADTOOLS23\Resources\Images directory and creates an AVI video from the still images.