Skip to content

Instantly share code, notes, and snippets.

@SamSaffron
Created May 29, 2012 06:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SamSaffron/2823028 to your computer and use it in GitHub Desktop.
Save SamSaffron/2823028 to your computer and use it in GitHub Desktop.
ThumbCreator
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Diagnostics;
using MediaBrowser.Library.Interop.DirectShowLib;
using MediaBrowser.Library.Logging;
namespace FrameGrabProvider
{
class ThumbCreator {
static readonly Guid videoType = new
System.Guid("73646976-0000-0010-8000-00AA00389B71");
public static bool CreateThumb(string videoFilename, string thumbFilename, double positionPercent)
{
Logger.ReportInfo("Creating thumb for " + videoFilename);
bool rval = false;
IMediaDet m = new MediaDet() as IMediaDet;
m.put_Filename(videoFilename);
int streamCount;
m.get_OutputStreams(out streamCount);
AMMediaType media_type = new AMMediaType();
for (int i = 0; i < streamCount; i++)
{
m.get_StreamMediaType(media_type);
VideoInfoHeader vih = (VideoInfoHeader)Marshal.PtrToStructure(media_type.formatPtr, typeof(VideoInfoHeader));
if (vih == null)
{
continue;
}
double pos;
m.get_StreamLength(out pos);
pos = (int)(pos * positionPercent);
int width = vih.BmiHeader.Width;
int height = vih.BmiHeader.Height;
if (height < 10 || width < 10)
{
continue;
}
string tempfile = Path.GetTempFileName() + ".bmp";
m.WriteBitmapBits(pos, width, height, tempfile);
if (File.Exists(tempfile))
{
using (var bitmap = new Bitmap(tempfile))
{
bitmap.Save(thumbFilename, ImageFormat.Png);
}
File.Delete(tempfile);
rval = true;
}
break;
}
Marshal.ReleaseComObject(m);
return rval;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment