Exemple #1
0
        /// <summary>
        /// Creates the html table
        /// </summary>
        /// <param name="source">The source path</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(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 ?? "");

                if (string.IsNullOrEmpty(source))
                {
                    OnInfo?.Invoke(GlobalHelper.InfoType.Error, "The source path is null or empty.");
                    return;
                }

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

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

                var imageFiles = GlobalHelper.GetImageFiles(source);


                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.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.Name}\" {imgSizeHtml} alt=\"{image.Name}\" title=\"{image.Name}\">"
                        : $"<img src=\"{image.Name}\" {imgSizeHtml} alt=\"{image.Name}\" title=\"{image.Name}\">";

                    htmlTable.AppendLine(
                        $"<td><a href=\"{image.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}");
            }
        }