Generate video thumbnail I wrote a simple, yet disgusting video thumbnail image generator method—which uses the MediaToolkit .NET library.

The code is not self-explanatory whatsoever, and it's extremely inefficient.

It accepts a video byte array &amp; MIME type—and writes it to a video file. Then, it obtains the video file's duration, stores the middle frame, and writes it to an image file. Finally, it retrieves the image file bytes and returns them ... after deleting the video &amp; image file.

How can I make it more efficient?

    public static byte[] GetVideoThumbnail(byte[] bytes, string mediaType)
    {
        var video = new MediaFile
        {
            Filename = Path.GetTempPath() + Guid.NewGuid() + MimeTypeMap.GetExtension(mediaType)
        };
        var image = new MediaFile { Filename = Path.GetTempPath() + Guid.NewGuid() + ".jpg" };
        File.WriteAllBytes(video.Filename, bytes);
        using (var engine = new Engine())
        {
            engine.GetMetadata(video);
            engine.GetThumbnail(video, image,
                new ConversionOptions { Seek = TimeSpan.FromSeconds(video.Metadata.Duration.TotalSeconds / 2) });
        }
        var thumbnail = File.ReadAllBytes(image.Filename);
        File.Delete(video.Filename);
        File.Delete(image.Filename);
        return thumbnail;
    }
The most inefficient thing in your code is usage of byte[] to hold video or image data. There is a concept of Large Object Heap in .NET which is not compacted unless explicitly otherwise made-to starting .Net 4.5.1. 

Red Gate's Explanation of Large Object Heap

Latest behavior of Large Object Heap

All your objects with a size of more that 85000 bytes are allocated on large object heap. These are not collected like  mentioned in .Net garbage collection and dealt with separately. You can read better in the links above. 

As a sidenote, You can cross-check if you do need engine.GetMetadata(video); to generate the thumbnail. 

The answer to your question would be 


Like you are returning image as a byte array, you can choose to return a image url from the disk. Some part of byte array will be reduced. 
With .net 4.5.1, you can call .net LOH compaction programmatically. 
You can also do this processing in completely separate appdomain in memory, and then unloading it. 
Or you can generate thumbnails from a completely separate windows service or console application, you simply restart them.


Read few more things on Large Object Heap