Click here to Skip to main content
12,550,447 members (45,456 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked

Getting Thumbnail from Video using MediaPlayer Class in WPF

, 21 Nov 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This article shows the use of MediaPlayer class to obtain thumbnails for video files.


I once needed to create thumbnails of videos to be used in a WPF application. First, I thought of using FFMPEG to get the job done, but then changed my mind and used System.Windows.Media.MediaPlayer after doing some search on the Internet.

I owe thanks to all the people from different places on the Internet from whom I derived this version of my application. I do not remember the places from where I had taken the code snippets. But I am thankful to all of them anyway!

To make things more interesting, the sample code creates thumbnails using multi-threading. We also show a nice animation while the thumbnail creation process is in progress.

MediaElement is a bit sluggish and takes some time before the video is loaded and we have to wait a little before acquiring the thumbnail. Unfortunately, the time to wait is quite arbitrary and varies from machine to machine and invocation to invocation. So I have provided a combo-box to change the wait time if the default does not work. I have also provided another combo-box to select the position in seconds from the start of the video to specify the time when the screenshot should be taken.

Here is a screenshot of the sample application at work:

The Code

Following is a simplified version of the accompanying sample application. I have placed comments where needed. The sample application is multi-threaded and uses just a little more elaborate code. Please see the sample code for more details.

void ImportMedia(string mediaFile, int waitTime, int position)
    MediaPlayer player = new MediaPlayer { Volume = 0, ScrubbingEnabled = true };
    player.Open(new Uri(mediaFile));
    player.Position = TimeSpan.FromSeconds(position);
    //We need to give MediaPlayer some time to load. 
    //The efficiency of the MediaPlayer depends                 
    //upon the capabilities of the machine it is running on and 
    //would be different from time to time
    System.Threading.Thread.Sleep(waitTime * 1000);

    //120 = thumbnail width, 90 = thumbnail height and 96x96 = horizontal x vertical DPI
    //In an real application, you would not probably use hard coded values!
    RenderTargetBitmap rtb = new RenderTargetBitmap(120, 90, 96, 96, PixelFormats.Pbgra32);
    DrawingVisual dv = new DrawingVisual();
    using (DrawingContext dc = dv.RenderOpen())
        dc.DrawVideo(player, new Rect(0, 0, 120, 90));
    Duration duration = player.NaturalDuration;
    int videoLength = 0;
    if (duration.HasTimeSpan)
        videoLength = (int)duration.TimeSpan.TotalSeconds;
    BitmapFrame frame = BitmapFrame.Create(rtb).GetCurrentValueAsFrozen() as BitmapFrame;
    BitmapEncoder encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(frame as BitmapFrame);
    MemoryStream memoryStream = new MemoryStream();
    //Here we have the thumbnail in the MemoryStream!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Lead
Pakistan Pakistan
I am a full-stack developer. My skills include JavaScript, C#/.Net, MS Azure cloud etc. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.

These days I am technical lead and chief programmer at Xtensio.

You may also be interested in...


Comments and Discussions

QuestionMessage Removed Pin
gtdev22-May-16 10:41
membergtdev22-May-16 10:41 
Questionurgent Pin
Dominic Hacker156-May-15 4:29
memberDominic Hacker156-May-15 4:29 
AnswerRe: urgent Pin
Kashif_Imran7-May-15 16:44
memberKashif_Imran7-May-15 16:44 
GeneralMy vote of 1 Pin
Boudi AlSayed26-Aug-14 3:12
memberBoudi AlSayed26-Aug-14 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 21 Nov 2012
Article Copyright 2012 by Kashif_Imran
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid