/// <summary> /// Creates a thumbnail of just the cover image (no title, language name, etc.) /// </summary> /// <returns>Returns true if successful; false otherwise. </returns> internal static bool CreateThumbnailOfCoverImage(Book.Book book, HtmlThumbNailer.ThumbnailOptions options, Action <Image> callback = null) { var imageSrc = book.GetCoverImagePath(); if (!IsCoverImageSrcValid(imageSrc, options)) { Debug.WriteLine(book.StoragePageFolder + " does not have a cover image."); return(false); } var size = Math.Max(options.Width, options.Height); var destFilePath = Path.Combine(book.StoragePageFolder, options.FileName); // Writing a transparent image to a file, then reading it in again appears to be the only // way to get the thumbnail image to draw with the book's cover color background reliably. var transparentImageFile = Path.Combine(Path.GetTempPath(), "Bloom", "Transparent", Path.GetFileName(imageSrc)); Directory.CreateDirectory(Path.GetDirectoryName(transparentImageFile)); try { if (RuntimeImageProcessor.MakePngBackgroundTransparent(imageSrc, transparentImageFile)) { imageSrc = transparentImageFile; } using (var coverImage = PalasoImage.FromFile(imageSrc)) { if (imageSrc == transparentImageFile) { coverImage.Image = MakeImageOpaque(coverImage.Image, book.GetCoverColor()); } var shouldAddDashedBorder = options.BorderStyle == HtmlThumbNailer.ThumbnailOptions.BorderStyles.Dashed; coverImage.Image = options.CenterImageUsingTransparentPadding ? ImageUtils.CenterImageIfNecessary(new Size(size, size), coverImage.Image, shouldAddDashedBorder) : ImageUtils.ResizeImageIfNecessary(new Size(size, size), coverImage.Image, shouldAddDashedBorder); switch (Path.GetExtension(destFilePath).ToLowerInvariant()) { case ".jpg": case ".jpeg": ImageUtils.SaveAsTopQualityJpeg(coverImage.Image, destFilePath); break; default: PalasoImage.SaveImageRobustly(coverImage, destFilePath); break; } if (callback != null) { callback(coverImage.Image.Clone() as Image); // don't leave GC to chance } } } finally { if (File.Exists(transparentImageFile)) { SIL.IO.RobustFile.Delete(transparentImageFile); } } return(true); }
// We are trying our best to end up with a thumbnail whose height/width ratio // is the same as the original image. This allows the Uploading and Already in Bloom Library // thumbs to top-align. private string ChooseBestUploadingThumbnailPath(Book.Book book) { // If this exists, it will have the original image's ratio of height to width. var thumb70Path = Path.Combine(book.FolderPath, "thumbnail-70.png"); if (RobustFile.Exists(thumb70Path)) { return(thumb70Path); } var coverImagePath = book.GetCoverImagePath(); if (coverImagePath == null) { return(book.ThumbnailPath); } else { RuntimeImageProcessor.GenerateThumbnail(book.GetCoverImagePath(), book.NonPaddedThumbnailPath, 70, ColorTranslator.FromHtml(book.GetCoverColor())); return(book.NonPaddedThumbnailPath); } }