Example #1
0
        /// <summary>
        /// 从指定路径提取视频文件缩略图
        /// </summary>
        /// <param name="sourcePath">视频路径</param>
        /// <param name="dstPath">存放图片路径</param>
        public void GetThumbnail(string sourcePath, string dstPath = @"./MediaLibraryTemp")
        {
            try
            {
                if (dstPath.Equals("./MediaLibraryTemp"))
                {
                    dstPath = MediaThumbnailTempPath;
                }
                if (!System.IO.Directory.Exists(dstPath))
                {
                    System.IO.Directory.CreateDirectory(dstPath);
                }
                string[] dsts = System.IO.Directory.GetFiles(dstPath);
                for (int i = 0; i < dsts.Length; i++)
                {
                    dsts[i] = dsts[i].Substring(dsts[i].LastIndexOf("\\") + 1);
                }

                Stack <string> dirs = new Stack <string>(200);

                if (!System.IO.Directory.Exists(sourcePath))
                {
                    throw new ArgumentException();
                }
                dirs.Push(sourcePath);
                while (dirs.Count > 0)
                {
                    string   currDir = dirs.Pop();
                    string[] subDirs;

                    try
                    {
                        subDirs = System.IO.Directory.GetDirectories(currDir);
                    }
                    catch (System.UnauthorizedAccessException e)
                    {
                        Console.WriteLine(e.Message);
                        continue;
                    }
                    catch (System.IO.DirectoryNotFoundException e)
                    {
                        Console.WriteLine(e.Message);
                        continue;
                    }

                    string[] files = null;
                    try
                    {
                        files = System.IO.Directory.GetFiles(currDir);
                    }
                    catch (System.UnauthorizedAccessException e)
                    {
                        Console.WriteLine(e.Message);
                        continue;
                    }
                    catch (System.IO.DirectoryNotFoundException e)
                    {
                        Console.WriteLine(e.Message);
                        continue;
                    }

                    foreach (string file in files)
                    {
                        try
                        {
                            System.IO.FileInfo fi         = new System.IO.FileInfo(file);
                            string             videoRegex = "(\\.mp4|\\.mkv|\\.rmvb|\\.mov|\\.flv|\\.avi|\\.mpg|\\.wmv|\\.mpeg)$";
                            //string audioRegex = "(\\.mp3|\\.wma)$";
                            string pictureRegex = "(\\.jpg|\\.bmp|\\.png|\\.tiff|\\.tif|\\.gif)$";

                            Regex r      = new Regex(videoRegex, RegexOptions.IgnoreCase);
                            Regex imageR = new Regex(pictureRegex, RegexOptions.IgnoreCase);
                            if (r.IsMatch(fi.Extension))
                            {
                                VideoItem vi = new VideoItem(file);
                                if (dsts.Contains(vi.name + vi.fileSize + ".jpg"))
                                {
                                    // Console.WriteLine(vi);
                                    continue;
                                }
                                vi.GetImageFromVedio(dstPath);
                                //Console.WriteLine(vi);
                            }
                            else if (imageR.IsMatch(fi.Extension))
                            {
                                ImageItem vi = new ImageItem(file);
                                if (dsts.Contains(vi.name + vi.fileSize.Trim() + ".jpg"))
                                {
                                    // Console.WriteLine(vi);
                                    continue;
                                }
                                vi.MakeThumbnail(dstPath + "\\" + vi.name + vi.fileSize.Trim() + ".jpg", 80, 80, "HW");
                                //Console.WriteLine(vi);
                            }
                        }
                        catch (System.IO.FileNotFoundException e)
                        {
                            Console.WriteLine(e.Message);
                            continue;
                        }
                    }

                    foreach (string str in subDirs)
                    {
                        dirs.Push(str);
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine(e);
            }
            catch (ArgumentException e)
            {
                return;
            }
        }