Image may be NSFW.
Clik here to view.Today there is again a bit of C# code. I’m writing about how you can create video files in C # from individual images or bitmaps. Using the AForge library, which I have used in the already in the C# webcam tutorial, this is going to be relatively simple.
Preparations
For the following tutorial you need the AForge.Video.FFMPEG.dll library and its FFMPEG libraries. Both can be found by clicking the below link. On the downloadpage you have to select that .zip-file, which contains “(libs only)” in it’s filename.
Now open your Visual Studio or an alternative program, that you want to use to write your program. (For the following tutorial I’m using Visual Studio.)
Create a project
Next you create a new project, I decided on a WinForms project, and wait until the Visual Studio has arranged everything. Unpack the previously downloaded AForge archive. In the release folder of the archive you will find all AForge libraries. However, for this tutorial, we only need the AForge.Video.FFMPEG library. Copy this into the folder which contains your Visual Studio project. Now add a reference the AForge.Video.FFMPEG library. (If you don’t know how to add references, have a look at this short article.)
Image may be NSFW.
Clik here to view.Before we go on, two other settings need to be made to your project. One the one hand you have to adjust the .NET framework target version. This must not exceed version 3.5, because the AForge libs we’re using aren’t compatible with .NET 4.0 & .NET 4.5. This changes can be made in the properties of your project. (See left-hand screenshot)
Image may be NSFW.
Clik here to view.On the other hand you may have to change the target architecture of your project. By default this should be set to “Any CPU”. Here you have to explicitly specify x86 as target architecture. You can also make this setting on the properties page of your project. (See right-hand screenshot)
Short status report – you should have created a project, added a reference to the AForge.Video.FFMPEG library, set the target platform to x86 and the target framework version to 3.5 or lower now. If so, it can go on.
In the next step, we need a few more dlls from the AForge archive. You can find these in the folder “Externals\ffmpeg” inside the AForge archive. All files in this folder have to be copied to the output folder of your Visual Studio project. (After we changed the target architecture this now should be “YourProjectFolder\bin\x86\Debug”.)
Beginning with the code
If you’ve also done this, we can start with the code. The first thing what you should do is to integrate the AForge library by writing the using directive.
using AForge.Video.FFMPEG;
In the next step we create a method in which want to write the C# code for video creation. For this reason I have created a button on the application’s main form and will subsequently use the button’s click method. (I know that this isn’t the perfect way, but it’s a good way to create a simple skeleton for the substantial code of this tutorial – the C# video writer code.)
private void buttonCreateVideo_Click(object sender, EventArgs e) { }
Create VideoFileWriter
Note: From now on all the following code snippets belong to this method. I will develop and explain the code step by step. Those, who only look for the solution, should now scroll to the end of this article.
First, we create two variables for the width and height of the video. We hereby specify the resolution of the video. Then we create an instance of the VideoFileWriter class and open a new video file using the open()-method.
int width = 640; int height = 480; VideoFileWriter writer = new VideoFileWriter(); writer.Open("en.code-bude_test_video.avi", width, height, 25, VideoCodec.MPEG4, 1000000);
A brief explanation of the open()-method. The fourth parameter specifies the frames per second of the video, the fifth specifies the videocodec and the sixth defines the bitrate of the video. The mentioned rate of 1000000 corresponds to a video bitrate of 1 Mbps. The higher the bitrate, the larger the video and the better the quality.
Writing the frames
Now we can arbitrarily write as many frames (in the form of bitmap graphics) as we like. That’s what the following code snippet does. By use of the modulus operator (%), and the random number generator, we will create different frames which will result in a nice animation. Thanks to the frame rate of 25 FPS, which we have stated above when we set up the open()-method, we know that 25 graphics result in 1 second of video. Since we are creating 375 bitmaps (frames) below, our video will be 15 seconds in length.
Bitmap image = new Bitmap(width, height); Graphics g = Graphics.FromImage(image); g.FillRectangle(Brushes.Black, 0, 0, width, height); Brush[] brushList = new Brush[] { Brushes.Green, Brushes.Red, Brushes.Yellow, Brushes.Pink, Brushes.LimeGreen }; Random rnd = new Random(); for (int i = 0; i < 250; i++) { int rndTmp = rnd.Next(1, 3); Application.DoEvents(); g.FillRectangle(brushList[i%5], (i % width)*2, (i % height) * 0.5f, i%30, i%30); g.FillRectangle(brushList[i % 5], (i % width)*2, (i % height)*2, i % 30, i % 30); g.FillRectangle(brushList[i % 5], (i % width) * 0.5f, (i % height) *2, i % 30, i % 30); g.Save(); writer.WriteVideoFrame(image); } g.DrawString("(c) 2013 by code-bude.net", new System.Drawing.Font("Calibri", 30), Brushes.White, 80, 240); g.Save(); for (int i = 0; i < 125; i++) { writer.WriteVideoFrame(image); }
Save the video file
In the last step we have to close the VideoFileWriter yet.
writer.Close();
That’s it. The few lines of code shown in this article are completely enough to create your very own video by using C#.
Download and overview
In the below listing there is once again the complete code of my project’s Form1.cs file. If you want you can also download the whole Visual Studio solution by clicking on the following link.
Download: VideoFileWriter solution
using AForge.Video.FFMPEG; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace VideoWriter { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void buttonCreateVideo_Click(object sender, EventArgs e) { int width = 640; int height = 480; VideoFileWriter writer = new VideoFileWriter(); writer.Open("code-bude_test_video.avi", width, height, 25, VideoCodec.MPEG4, 1000000); Bitmap image = new Bitmap(width, height); Graphics g = Graphics.FromImage(image); g.FillRectangle(Brushes.Black, 0, 0, width, height); Brush[] brushList = new Brush[] { Brushes.Green, Brushes.Red, Brushes.Yellow, Brushes.Pink, Brushes.LimeGreen }; Random rnd = new Random(); for (int i = 0; i < 250; i++) { int rndTmp = rnd.Next(1, 3); Application.DoEvents(); g.FillRectangle(brushList[i%5], (i % width)*2, (i % height) * 0.5f, i%30, i%30); g.FillRectangle(brushList[i % 5], (i % width)*2, (i % height)*2, i % 30, i % 30); g.FillRectangle(brushList[i % 5], (i % width) * 0.5f, (i % height) *2, i % 30, i % 30); g.Save(); writer.WriteVideoFrame(image); } g.DrawString("(c) 2013 by code-bude.net", new System.Drawing.Font("Calibri", 30), Brushes.White, 80, 240); g.Save(); for (int i = 0; i < 125; i++) { writer.WriteVideoFrame(image); } writer.Close(); } } }