Overlay Text On Video Using Expression Encoder 4
I recently built an application where I wanted to write some arbitrary text onto a video. I was using Expression Encoder 4 and had a difficult time finding some examples and documentation, aside from some basic MSDN documentation on the MediaItem class. I was eventually able to figure it out, but it took a little time. Even though the end result is not particularly glamorous, I thought I’d save you some time and share what I learned.
The first thing to recognize is that you have to create a Bitmap of the text you want to overlay. This Bitmap is then overlayed on to the MediaItem. So, first things first, create your Bitmap. Here’s an approach:
- /// <summary>
- /// This method will create a bitmap based
- /// </summary>
- /// <param name="overlayText"></param>
- /// <param name="rootPath"></param>
- /// <param name="width"></param>
- /// <param name="height"></param>
- /// <returns></returns>
- private static string createOverlayImage(string overlayText, string rootPath, int width, int height)
- {
- // full path for a temporary bitmap
- string overlayFileName = rootPath + "\\" + Guid.NewGuid().ToString() + ".bmp";
- // create a bitmap
- Bitmap bitmap = new Bitmap(width, height);
- Graphics g = Graphics.FromImage(bitmap);
- // define the font
- Font font = new Font("Arial", (float)14.0);
- // define the area to draw on
- Rectangle area = new Rectangle(new Point(0, 0), new Size(width, height));
- // draw the new image
- g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
- g.DrawString(overlayText, font, Brushes.Red, area);
- // save the picture with the text overlay
- bitmap.Save(overlayFileName);
- // return the path to the overlay image
- return overlayFileName;
- }
Nothing too surprising here:
- Line #9: Pass in the text you want to overlay, the path for where you’ll store the created bitmap, and the width/height.
- Line #12: Create a random file name for the Bitmap.
- Line #26: You can change the font, color, and locations here if you’d like.
Now, with this method, you can set the overlay properties on the MediaItem like this:
- // sets file name to media item
- mediaItem = new MediaItem("test.wmv");
- // create the overlay image and return the path
- string overlayFileName = createOverlayImage("Thank you for encoding this video!", Environment.CurrentDirectory, mediaItem.VideoSize.Width, mediaItem.VideoSize.Height);
- // create the overlay on the media item
- mediaItem.OverlayFileName = overlayFileName;
- mediaItem.OverlayLayoutMode = OverlayLayoutMode.WholeSequence;
- mediaItem.OverlayRect = new Rectangle(new Point(30, 10), new Size((mediaItem.VideoSize.Width – 30), (mediaItem.VideoSize.Height – 10)));
Breaking it down:
- Line #5: Grab the full path to the newly created Bitmap over your text overlay.
- Line #8: Set the OverlayFileName to your Bitmap.
- Line #9: Choose the layout mode. There are a few options here that you can explore; details are on MSDN.
- Line #10: The OverlayRect defines where your Bitmap lives on the video. I indented it a bit, but it’s up to you.
I’ve modified the Simple template provided by the Expression Encoder 4 SDK with the code. You can find it below. Hope this helps.
[...] About « Overlay Text On Video Using Expression Encoder 4 [...]
[...] This post was mentioned on Twitter by MSExpression, Rodolpho Sa, Emmanuel yazid, Antonio Raga, Kunal Chowdhury and others. Kunal Chowdhury said: RT @MSExpression: Overlay Text On Video Using #Expression #Encoder 4 – http://bit.ly/e7w2ym – #msexp [...]
Is there a way to feed MediaItem a livejob ?
MediaItem mediaItem = new MediaItem(“test.wmv”);
If you’re using the EE4 GUI, you can do this with a XAML overlay… I’ve used this for doing burn-in captions (source at https://github.com/i-e-b/TimedTextToXaml ). Not sure if this can be done with the SDK, but I’d guess so.