コード例 #1
0
ファイル: MainForm.cs プロジェクト: Benguan/M3
        private void Build()
        {
            try
            {
                if (sourceGallery == null)
                {
                    sourceGallery = new Gallery { Categories = new List<Category>() };
                }

                var gallery = new Gallery { Categories = new List<Category>() };

                var startTime = DateTime.Now;
                var year = 0;
                var folders = sourceFolder.GetDirectories();

                var thumbnailMaxWidth = ConfigurationManager.ThumbnailBuilderConfiguration.ThumbnailMaxWidth;
                var thumbnailMaxHeight = ConfigurationManager.ThumbnailBuilderConfiguration.ThumbnailMaxHeight;
                var photoMaxWidth = ConfigurationManager.ThumbnailBuilderConfiguration.PhotoMaxWidth;
                var photoMaxHeight = ConfigurationManager.ThumbnailBuilderConfiguration.PhotoMaxHeight;

                var photosPath = Path.Combine(DirectoryHelper.GetSolutionDirectoryPath(AppDomain.CurrentDomain.BaseDirectory), ConfigurationManager.ThumbnailBuilderConfiguration.TargetFolderPath);

                foreach (var folder in folders)
                {
                    var files = folder.GetFiles("*.jpg", SearchOption.AllDirectories);
                    var folderName = folder.Name;
                    var photoId = 0;
                    var category = new Category
                    {
                        Photos = new List<Photo>()
                    };

                    var name = GetNameWithoutDateInfo(folderName);

                    if (IsExistInGallery(name))
                    {
                        for (var i = 0; i < files.Length; i++)
                        {
                            this.InvokeProcessBar();
                        }
                        continue;
                    }

                    if (files.Length <= 0)
                    {
                        break;
                    }

                    try
                    {
                        var image = new Bitmap(files[0].FullName);
                        var propertyItem = image.GetPropertyItem(0x132);
                        var dateString = Encoding.UTF8.GetString(propertyItem.Value, 0, propertyItem.Value.Length - 1);
                        var date = DateTime.ParseExact(dateString, "yyyy:MM:dd HH:mm:ss", CultureInfo.InvariantCulture);
                        year = date.Year;
                        category.Date = date.ToString("yyyy-MM-dd");
                        image.Dispose();
                    }
                    catch (ArgumentException)
                    {
                        year = files[0].LastWriteTime.Year;
                        category.Date = files[0].LastWriteTime.ToString("yyyy-MM-dd");
                    }
                    catch (Exception)
                    {
                        category.Date = DateTime.MinValue.ToString("yyyy-MM-dd");
                        year = 0;
                    }

                    category.Year = year;
                    category.Name = name;
                    gallery.Categories.Add(category);

                    foreach (var file in files)
                    {


                        var thumbnailFileNameWithFolder = year + file.FullName.Substring(sourceFolder.FullName.Length, file.FullName.Length - sourceFolder.FullName.Length);
                        var thumbnailFullPath = Path.Combine(photosPath, thumbnailFolder, thumbnailFileNameWithFolder);
                        var thumbnailPhotoInfo = ImageHelper.GetThumbnail(thumbnailMaxWidth, thumbnailMaxHeight, file.FullName, thumbnailFullPath, false);
                        var normalFileNameWithFolder = year + file.FullName.Substring(sourceFolder.FullName.Length, file.FullName.Length - sourceFolder.FullName.Length);
                        var normalFullPath = Path.Combine(photosPath, normalFolder, normalFileNameWithFolder);
                        var normalPhotoInfo = ImageHelper.GetThumbnail(photoMaxWidth, photoMaxHeight, file.FullName, normalFullPath, false);

                        photoId++;
                        var photo = new Photo
                            {
                                Id = photoId,
                                Title = Path.GetFileNameWithoutExtension(file.FullName),
                                Height = normalPhotoInfo.Height,
                                Width = normalPhotoInfo.Width,
                                NormalUrl = "/Resources/images/photos/" + normalFolder + "/" + StringHelper.GetUriFromPath(normalFileNameWithFolder),
                                ThumbnailUrl = "/Resources/images/photos/" + thumbnailFolder + "/" + StringHelper.GetUriFromPath(thumbnailFileNameWithFolder)
                            };

                        category.Photos.Add(photo);
                        this.InvokeProcessBar();
                    }
                }

                int categoryId = sourceGallery.MaxId;

                foreach (var category in gallery.Categories)
                {
                    categoryId++;
                    category.Id = categoryId;
                }

                if (gallery.Categories != null && gallery.Categories.Count > 0)
                {
                    sourceGallery.Categories.AddRange(gallery.Categories);
                }

                sourceGallery.Categories.Sort();

                int pageId = 0;
                foreach (var category in sourceGallery.Categories)
                {
                    pageId++;
                    category.Page = pageId;
                }

                try
                {
                    StorageHelper.SaveGallery(sourceGallery);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("SaveGallery Error:" + ex.Message);
                }


                MessageBox.Show(string.Format("Done!\n共转换{0}个文件,耗时{1}秒", doneFilesCount, (DateTime.Now - startTime).TotalSeconds), "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Build Error:" + ex.Message);
            }
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: Benguan/M3
 public MainForm()
 {
     InitializeComponent();
     SourceFolderTextBox.Text = ConfigurationManager.ThumbnailBuilderConfiguration.SourceFolderPath;
     sourceGallery = StorageHelper.GetGallery();
 }