示例#1
0
        public static Category GetPagedCategory(int categoryId, bool fill)
        {
            var categories = StorageHelper.GetAllCategories();

            var category = categories.Find(c => c.Id == categoryId);

            var pagedCategory = new Category
                {
                    Id = category.Id,
                    Name = category.Name,
                    Year = category.Year,
                    Page = category.Page,
                    Cover = category.Photos[0],
                    Photos = category.Photos
                };

            return pagedCategory;
        }
示例#2
0
        public Category Preview(int id)
        {
            var gallery = StorageHelper.GetGallery();
            var category = gallery.Categories.Find(c => c.Id == id);

            var previewSize = ConfigurationManager.WebsiteConfiguration.PreviewCategorySize <= category.Photos.Count ?
                ConfigurationManager.WebsiteConfiguration.PreviewCategorySize : category.Photos.Count;

            var previewCategory = new Category
            {
                Id = category.Id,
                Name = category.Name,
                Year = category.Year
            };

            previewCategory.Photos = ListHelper.GetRandomOrder<Photo>(category.Photos).GetRange(0, previewSize);

            return previewCategory;
        }
示例#3
0
        public static Category GetCategoryByPage(int categoryPage, int photoPage, bool fill)
        {
            var categories = StorageHelper.GetAllCategories();

            var category = categories.Find(c => c.Page == categoryPage);
            var pageSize = ConfigurationManager.WebsiteConfiguration.DetailPageSize;
            var photosCount = category.Photos.Count;
            var currentPageSize = pageSize;
            var startId = pageSize * (photoPage - 1);
            if (startId > photosCount)
            {
                return null;
            }
            var maxFullPage = category.Photos.Count / pageSize;
            if (photoPage > maxFullPage)
            {
                currentPageSize = category.Photos.Count - pageSize * (photoPage - 1);
            }
            var pagedCategory = new Category
            {
                Id = category.Id,
                Name = category.Name,
                Year = category.Year,
                Page = category.Page,
                Cover = category.Photos[0],
                Photos = category.Photos.GetRange(startId, currentPageSize)
            };

            if (fill)
            {
                for (var i = 0; i < pageSize - currentPageSize; i++)
                {
                    pagedCategory.Photos.Add(null);
                }
            }
            return pagedCategory;
        }
示例#4
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);
            }
        }