Issues trying to get a thumbnail extracted from a MP4 video
Posted: Thu Nov 13, 2025 6:47 pm
I am trying to extract a thumbnail from a MP4 file. I wonder if anybody has tried this and could tell me what's wrong.
As there's no built in method in Windows, like there is for pictures, I started with AI generated C# code. Not X# code unfortunately as AI won't supply that. But hopefully someone has an idea of what causes the issue; StackOverflow seems more logical but I rarely had any results there in the past.
The original code assigns a control as follows:
Issue 1
Here the problem is I get Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown, directly after the wmp.URL= filePath; Even after a (long) waiting time.
I have no idea how to get the required ActiveState (=Playing) by code. Remarkable detail is that I get the same exception from the ImmediateWindow UNLESS I first type wmp or ?wmp, showing 100 properties of the control. After that the program can continue without the exception. How can I do programmatically what this IW command achieves?
It doesn't make a difference to run it from a new Thread.
I have also tried to insert this code:
But the state of the control remains wmppsTransitioning , also after this 5 seconds.
Issue 2
After having issue 1, I decided to capture from the WindowsMedia Player control on a Winforms form after I press a Start button. The video starts playing and I found out that the video isn't played before the whole method behind the button ends. So I added a new button calling both 12 year old code and the modern code I got from AI. The result is that the old code saves a picture, not completely as I want it as the controls of the MediaPlayer are included. Still the 'new' code keeps saving a black screen. See below for the code.
When I run the 2 Capture methods after starting the video, I get the a black screen in both (=issue 1)
Obviously I want the capture to run without the need for a user to click a button after starting a video and also I would prefer to use the 2nd method without ending up with a black screen. Any help is appreciated.
Dick
As there's no built in method in Windows, like there is for pictures, I started with AI generated C# code. Not X# code unfortunately as AI won't supply that. But hopefully someone has an idea of what causes the issue; StackOverflow seems more logical but I rarely had any results there in the past.
The original code assigns a control as follows:
Code: Select all
private static Bitmap ExtractThumbnailTryFromAI(string filePath, TimeSpan position)
{
Bitmap bitmap = null;
//var thread = new Thread(() =>
//{
var wmp = new AxWMPLib.AxWindowsMediaPlayer();
var form = new Form();
form.Controls.Add(wmp);
wmp.CreateControl();
wmp.URL= filePath;
wmp.settings.autoStart = false;
wmp.Ctlcontrols.currentPosition = position.TotalSeconds;
wmp.Ctlcontrols.play();
wmp.Ctlcontrols.pause();
bitmap = new Bitmap(wmp.Width, wmp.Height); // Capture the video frame from the control
wmp.DrawToBitmap(bitmap, new Rectangle(0, 0, wmp.Width, wmp.Height));
form.Dispose();
}
Here the problem is I get Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown, directly after the wmp.URL= filePath; Even after a (long) waiting time.
I have no idea how to get the required ActiveState (=Playing) by code. Remarkable detail is that I get the same exception from the ImmediateWindow UNLESS I first type wmp or ?wmp, showing 100 properties of the control. After that the program can continue without the exception. How can I do programmatically what this IW command achieves?
It doesn't make a difference to run it from a new Thread.
I have also tried to insert this code:
Code: Select all
int wait = 0; // Wait for the media to be ready and frame to be rendered
while (wmp.playState != WMPLib.WMPPlayState.wmppsPlaying && wait < 5000)
{
Thread.Sleep(100);
wait += 100;
}
Issue 2
After having issue 1, I decided to capture from the WindowsMedia Player control on a Winforms form after I press a Start button. The video starts playing and I found out that the video isn't played before the whole method behind the button ends. So I added a new button calling both 12 year old code and the modern code I got from AI. The result is that the old code saves a picture, not completely as I want it as the controls of the MediaPlayer are included. Still the 'new' code keeps saving a black screen. See below for the code.
When I run the 2 Capture methods after starting the video, I get the a black screen in both (=issue 1)
Obviously I want the capture to run without the need for a user to click a button after starting a video and also I would prefer to use the 2nd method without ending up with a black screen. Any help is appreciated.
Dick
Code: Select all
private void CaptureNowNewMethod()
{
Bitmap bitmap = null;
var tPos=TimeSpan.FromSeconds(1);
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = tPos.TotalSeconds;
axWindowsMediaPlayer1.Ctlcontrols.pause();
bitmap = new Bitmap(axWindowsMediaPlayer1.Width,axWindowsMediaPlayer1.Height); // Capture the video frame
axWindowsMediaPlayer1.DrawToBitmap(bitmap,new Rectangle(0,0,axWindowsMediaPlayer1.Width,axWindowsMediaPlayer1.Height));
if(bitmap != null)
{
string outputFile = Path.Combine("c:\\temp\\","PicFromNewMethod.bmp");
bitmap.Save(outputFile,ImageFormat.Bmp);
}}
private void CaptureNowOldMethod()
{
if(!string.IsNullOrEmpty(axWindowsMediaPlayer1.URL))
{
var tPos=TimeSpan.FromSeconds(1);
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = tPos.TotalSeconds;
axWindowsMediaPlayer1.Ctlcontrols.pause();
System.Drawing.Image ret = null;
try
{
Bitmap bitmap = new Bitmap(axWindowsMediaPlayer1.Width,axWindowsMediaPlayer1.Height);
{
Graphics g = Graphics.FromImage(bitmap);
{
Graphics gg = axWindowsMediaPlayer1.CreateGraphics();
{
this.BringToFront();
g.CopyFromScreen(
axWindowsMediaPlayer1.PointToScreen(
new System.Drawing.Point()).X,
axWindowsMediaPlayer1.PointToScreen(
new System.Drawing.Point()).Y,
0,0,
new System.Drawing.Size(
axWindowsMediaPlayer1.Width,
axWindowsMediaPlayer1.Height)
);
}
}
{
using(MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
ret = System.Drawing.Image.FromStream(ms);
string outputFile = Path.Combine("c:\\temp\\","PicFromOldMethod.bmp");
ret.Save(outputFile);
}}}
}
catch(Exception ex){}
}}