Example #1
0
        /// ------------------------------------------------------------------------------------
        public static Image GetImageFromVideo(string videoPath, float seconds)
        {
            Image img = null;

            videoPath = videoPath.Replace('\\', '/');
            var prs       = new ExternalProcess(MPlayerPath);
            var tmpFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            Directory.CreateDirectory(tmpFolder);
            var tmpFile = Path.Combine(tmpFolder, "00000001.jpg");

            try
            {
                prs.StartInfo.Arguments =
                    string.Format("-nocache -nofontconfig -really-quiet -frames 1 -ss {0} -nosound -vo jpeg:outdir=\"\"\"{1}\"\"\" \"{2}\"",
                                  seconds, tmpFolder, videoPath);

                prs.StartProcess();
                prs.WaitForExit();
                prs.Close();

                // I'm hesitant to comment out this line, but because of SP-248, we'll see what happens.
                //ComponentFile.WaitForFileRelease(videoPath);

                if (File.Exists(tmpFile))
                {
                    // I could use Image.FromFile, but that leaves
                    // a lock on the file, for some reason.
                    var stream = new FileStream(tmpFile, FileMode.Open);
                    img = Image.FromStream(stream);
                    stream.Close();
                }
            }
            finally
            {
                if (File.Exists(tmpFile))
                {
                    File.Delete(tmpFile);
                }

                try { Directory.Delete(tmpFolder); }
                catch { }
            }

            return(img);
        }