Пример #1
0
        /// <summary>
        /// Creates thumbnails for the images, wich are stored in the given source
        /// </summary>
        /// <param name="imageFiles">The image files</param>
        /// <param name="source">The path of the source folder</param>
        /// <param name="width">The width of the thumbnail</param>
        /// <param name="height">The height of the thumbnail</param>
        /// <param name="keepRatio">true if the user wants to keep the ratio of the image, otherwise false</param>
        /// <returns>A dictionary with the image size for every image. The key is the name of the image</returns>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="DirectoryNotFoundException"/>
        public static Dictionary <string, ImageSize> CreateThumbnails(List <ImageModel> imageFiles, string source, int width, int height, bool keepRatio)
        {
            // Step 1: Check the parameters
            if (!Directory.Exists(source))
            {
                throw new DirectoryNotFoundException("The specified source doesn't exist.");
            }

            var result = new Dictionary <string, ImageSize>();

            // Step 2: Create Thumbnail folder
            if (!CreateThumbnailFolder(source))
            {
                OnNewInfo?.Invoke(GlobalHelper.InfoType.Error, "Can't create thumbnail folder.");
                return(result);
            }

            // Step 4: Itterate through the image list and create for every image a thumbnail
            var count = 1;

            foreach (var image in imageFiles)
            {
                OnProgress?.Invoke(GlobalHelper.CalculateCurrentProgress(count++, imageFiles.Count), 100);
                OnNewInfo?.Invoke(GlobalHelper.InfoType.Info, $"Create thumbnail {count} of {imageFiles.Count}");

                var imageSize = new ImageSize(width, height);
                if (keepRatio || height == 0 && width != 0 || height != 0 && width == 0)
                {
                    imageSize = CalculateImageSize(image.File, width, height);
                }

                var newImage = GlobalHelper.ResizeImage(image.File, imageSize.Width, imageSize.Height);

                newImage.Save(Path.Combine(source, ThumbnailFolderName, image.File.Name));

                result.Add(image.File.Name, imageSize);
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Creates a download able archive
        /// </summary>
        /// <param name="source">The file source</param>
        /// <param name="archiveName">The archive name</param>
        /// <returns>true if successful, otherwise false</returns>
        private static bool CreateArchive(string source, string archiveName)
        {
            try
            {
                // Calculates the percent
                string CalculatePercent(int step, int max)
                {
                    return($"{100d / max * step:N2}");
                }

                var archivePath = Path.Combine(source, archiveName);
                if (File.Exists(archivePath))
                {
                    File.Delete(archivePath);
                }

                var count = 1;
                using (var zipFile = new ZipFile())
                {
                    var files = GlobalHelper.GetImageFiles(source);

                    foreach (var file in files)
                    {
                        OnInfo?.Invoke(GlobalHelper.InfoType.Info, $"Create archive ({CalculatePercent(count++, files.Count)}%)");
                        OnProgress?.Invoke(GlobalHelper.CalculateCurrentProgress(count, files.Count), 100);
                        zipFile.AddFile(file.File.FullName);
                    }

                    zipFile.Save(archivePath);
                }

                return(true);
            }
            catch (Exception ex)
            {
                OnInfo?.Invoke(GlobalHelper.InfoType.Error, $"An error has occured while creating the archive. Message: {ex.Message}");
                return(false);
            }
        }
Пример #3
0
        /// <summary>
        /// Creates the html table
        /// </summary>
        /// <param name="imageFiles">The image files</param>
        /// <param name="source">The path of the source folder</param>
        /// <param name="createThumbnails">The value which indicates if the user want to use thumbnails</param>
        /// <param name="thumbHeight">The height of the thumbnail</param>
        /// <param name="thumbWidth">The width of the thumbnail</param>
        /// <param name="keepRatio">The value which indicates if the ratio should be keeped</param>
        /// <param name="headerText">The headertext</param>
        /// <param name="blankTarget">true to use a blank target</param>
        /// <param name="columnCount">The column count</param>
        /// <param name="imageFooter">The image footer id</param>
        /// <param name="createArchive">true if the user wants to create a archive</param>
        /// <param name="archiveName">The name of the archive</param>
        /// <param name="openPage">true when the pages should be opened at the end</param>
        public static void CreateHtmlTable(List <ImageModel> imageFiles, string source, bool createThumbnails, int thumbHeight, int thumbWidth,
                                           bool keepRatio, string headerText, bool blankTarget, int columnCount, FooterType imageFooter, bool createArchive, string archiveName, bool openPage)
        {
            try
            {
                archiveName = CreateArchiveName(archiveName ?? "");

                var imageSizeList = new Dictionary <string, ImageSize>();
                if (createThumbnails)
                {
                    ThumbnailManager.OnNewInfo  += ThumbnailManagerOnOnNewInfo;
                    ThumbnailManager.OnProgress += ThumbnailManager_OnProgress;
                    imageSizeList = ThumbnailManager.CreateThumbnails(imageFiles, source, thumbWidth, thumbHeight, keepRatio);
                    ThumbnailManager.OnProgress -= ThumbnailManager_OnProgress;
                    ThumbnailManager.OnNewInfo  -= ThumbnailManagerOnOnNewInfo;
                }

                var htmlTable = new StringBuilder("");
                var target    = blankTarget ? "target=\"_blank\"" : "";

                var count      = 1;
                var totalCount = 1;

                foreach (var image in imageFiles)
                {
                    OnInfo?.Invoke(GlobalHelper.InfoType.Info, $"Create image entry {totalCount} of {imageFiles.Count}");
                    OnProgress?.Invoke(GlobalHelper.CalculateCurrentProgress(totalCount, imageFiles.Count), 100);
                    if (count == 1)
                    {
                        htmlTable.AppendLine("<tr>");
                    }


                    if (!imageSizeList.TryGetValue(image.File.Name, out var imgSize))
                    {
                        imgSize = ImageSize.CreateDefault();
                    }

                    // Create the tag for the size
                    var imgSizeHtml = $"width=\"{imgSize.Width}\" height=\"{imgSize.Height}\"";

                    // Create the image tag
                    var thumbnail = createThumbnails
                        ? $"<img src=\"thumbnails/{image.File.Name}\" {imgSizeHtml} alt=\"{image.File.Name}\" title=\"{image.File.Name}\">"
                        : $"<img src=\"{image.File.Name}\" {imgSizeHtml} alt=\"{image.File.Name}\" title=\"{image.File.Name}\">";

                    htmlTable.AppendLine(
                        $"<td><a href=\"{image.File.Name}\" {target}>{thumbnail}</a>{CreateImageFooter(image, imageFooter, totalCount, imageFiles.Count)}</td>");

                    count++;
                    totalCount++;

                    if (count <= columnCount)
                    {
                        continue;
                    }
                    count = 1;
                    htmlTable.AppendLine("</tr>");
                }

                var archiveHtml = "";
                if (createArchive)
                {
                    if (CreateArchive(source, archiveName))
                    {
                        archiveHtml = $"You can download all pictures here: <a href=\"{CreateArchiveName(archiveName)}\">{CreateArchiveName(archiveName)}</a><br /><br />";
                    }
                }

                OnInfo?.Invoke(GlobalHelper.InfoType.Info, "Write data into file.");
                var indexPath = Path.Combine(source, "index.html");
                File.WriteAllText(indexPath, CreateFinaleHtml(headerText, htmlTable.ToString(), archiveHtml));

                if (File.Exists(indexPath))
                {
                    OnInfo?.Invoke(GlobalHelper.InfoType.Info, "File created.");
                    if (openPage)
                    {
                        Process.Start(indexPath);
                    }
                }
                else
                {
                    OnInfo?.Invoke(GlobalHelper.InfoType.Error, "Can't create file.");
                }
            }
            catch (Exception ex)
            {
                OnInfo?.Invoke(GlobalHelper.InfoType.Error, $"An error has occured. Message: {ex.Message}");
            }
        }