By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,460 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,460 IT Pros & Developers. It's quick & easy.

bitmap from video file source available?

P: n/a

Hi,

I get an infinite loop and non-"optimal" results from an MSDN Magazine March 2007 article on creating bitmaps from video files.

The infinite loop and non-"optimal" results are from a Microsoft Support Technician.

Does anyone have better code or better workarounds?

Here is my code:

private void CreateThumbnail(string file, string outputDir)
{
try
{
MediaPlayer mp = new MediaPlayer();
mp.Open(new Uri(file));
mp.Position = new TimeSpan(00, 00, 00);
Workaround(mp);

RenderTargetBitmap rtb = new RenderTargetBitmap(260, 184, 1 / 200, 1 / 200, PixelFormats.Pbgra32);

DrawingVisual dv = new DrawingVisual();
DrawingContext dc = dv.RenderOpen();
RectAnimation ra = new RectAnimation();
dc.DrawVideo(mp, new Rect(0, 0, 260, 184));

dc.Close();
rtb.Render(dv);

string filename = GetFileName(file);

//create the output filename
object[] formatParams = new object[2];
formatParams[0] = outputDir;
formatParams[1] = filename;

string outputFileName = null;

outputFileName = string.Format("{0}\\{1}_thumbnail.bmp", formatParams);

BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));

using (FileStream writer = new FileStream(outputFileName, FileMode.Create))
{
System.Console.WriteLine("Saving [{0}]", outputFileName);
encoder.Save(writer);
}
mp.Close();
mp = null;
}
catch (Exception e)
{
System.Console.WriteLine("CreateThumbnail: " + e.Message);
System.Environment.Exit(1);
}
}

private void Workaround(MediaPlayer mp)
{
mp.Play();
TimeSpan startPosition = mp.Position;
while (startPosition == mp.Position)
{
System.Threading.Thread.Sleep(1000);
}
mp.Pause();
}
Regards,
williamj
Jul 3 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.