471,350 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

bitmap from video file source available?


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
0 1027

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Gandalf | last post: by
7 posts views Thread by Dennis | last post: by
8 posts views Thread by Nathan Sokalski | last post: by
7 posts views Thread by Nathan Sokalski | last post: by
8 posts views Thread by gsmith | last post: by
3 posts views Thread by triplejump24 | last post: by
2 posts views Thread by ajay_itbhu | last post: by
4 posts views Thread by cronusf | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.