/// <summary> /// Generates an image for the media file at the specified <paramref name="sourceFilePath" /> and returns the output from the /// execution of the convert utility. The thumbnail is saved to <paramref name="destFilePath" />. The <paramref name="galleryId" /> /// is used during error handling to associate the error, if any, with the gallery. Requires the application to be running at /// Full Trust and GhostScript to be installed on the server. Returns <see cref="String.Empty" /> when the /// application is running at less than Full Trust or when the convert utility is not present in the bin directory. /// </summary> /// <param name="sourceFilePath">The full file path to the source media file. Example: D:\media\myfile.eps</param> /// <param name="destFilePath">The full file path to store the image to. If a file with this name is already present, /// it is overwritten.</param> /// <param name="galleryId">The gallery ID.</param> /// <returns>Returns the text output from the execution of the convert.exe utility.</returns> public static string GenerateImage(string sourceFilePath, string destFilePath, int galleryId) { string convertOutput = String.Empty; if ((AppSetting.Instance.AppTrustLevel != ApplicationTrustLevel.Full) || (String.IsNullOrEmpty(AppSetting.Instance.ImageMagickPathResolved))) { return(convertOutput); } // Create arguments. The [0] tells it to generate one image from the first page for PDF files (otherwise we get one image for every page) // Example: "D:\media\pic.eps[0]" "D:\media\pic.jpg" var args = String.Format(CultureInfo.InvariantCulture, @"""{0}[0]"" ""{1}""", sourceFilePath, destFilePath); var imageMagick = new ImageMagick(); convertOutput = imageMagick.ExecuteConvert(args, galleryId); if (!String.IsNullOrEmpty(convertOutput)) { if (System.IO.File.Exists(destFilePath)) { // ImageMagick successfully created the file but still returned output - probably warnings. Record as an informational event. var msg = String.Format("ImageMagick (convert.exe) successfully processed the file {0} but returned some information you may want to review. See below.", sourceFilePath); var data = new System.Collections.Generic.Dictionary <string, string> { { "convert.exe args", args }, { "convert.exe output", convertOutput } }; Events.EventController.RecordEvent(msg, EventType.Info, galleryId, Factory.LoadGallerySettings(), AppSetting.Instance, data); } else { // ImageMagick couldn't create the file. Log this as an error. var ex = new BusinessException(String.Format("ImageMagick (convert.exe) threw an error while trying to generate an image for the file {0}.", sourceFilePath)); ex.Data.Add("convert.exe args", args); ex.Data.Add("convert.exe output", convertOutput); Events.EventController.RecordError(ex, AppSetting.Instance, galleryId, Factory.LoadGallerySettings()); } } return(convertOutput); }
/// <summary> /// Creates an image file using ImageMagick having a max length of <paramref name="maxLength" /> and JPEG quality of /// <paramref name="jpegQuality" /> from the original file of <see cref="GalleryObject" />. The file is saved to the location /// <paramref name="newFilePath" />. The width and height of the generated image is returned as a <see cref="ISize" /> instance. /// </summary> /// <param name="newFilePath">The full path where the image will be saved.</param> /// <param name="maxLength">The maximum length of one side of the image.</param> /// <param name="jpegQuality">The JPEG quality.</param> /// <returns>Returns a <see cref="ISize" /> instance containing the width and height of the generated image.</returns> protected ISize GenerateImageUsingImageMagick(string newFilePath, int maxLength, int jpegQuality) { // Generate a temporary filename to store the thumbnail created by ImageMagick. string tmpImagePath = Path.Combine(AppSetting.Instance.TempUploadDirectory, String.Concat(Guid.NewGuid().ToString(), ".jpg")); if (!String.IsNullOrEmpty(GalleryObject.Original.TempFilePath)) { // Use the image that was created earlier in the thumbnail generator. tmpImagePath = GalleryObject.Original.TempFilePath; } // Request that ImageMagick create the image. If successful, the file will be created. If not, it fails silently. if (!File.Exists(tmpImagePath)) { ImageMagick.GenerateImage(GalleryObject.Original.FileNamePhysicalPath, tmpImagePath, GalleryObject.GalleryId); } if (File.Exists(tmpImagePath)) { // Save the path so it can be used later by the optimized image creator. GalleryObject.Original.TempFilePath = tmpImagePath; try { // ImageMagick successfully created an image. Now resize it to the width and height we need. // We can safely use the WPF version since we'll only get this far if we're running in Full Trust. //return GenerateImageUsingWpf(tmpImagePath, newFilePath, maxLength, jpegQuality); return GenerateImageUsingDotNet(newFilePath, maxLength, jpegQuality); } catch (Exception ex) { ex.Data.Add("GSP Info", String.Format("This error occurred while trying to process the ImageMagick-generated file {0}. The original file is {1}. The gallery will try to create an image using .NET instead.", tmpImagePath, GalleryObject.Original.FileNamePhysicalPath)); EventController.RecordError(ex, AppSetting.Instance, GalleryObject.GalleryId, Factory.LoadGallerySettings()); return Size.Empty; } } return Size.Empty; }
private void GenerateThumbnailImageUsingImageMagick(string newFilePath, IGallerySettings gallerySetting) { // Generate a temporary filename to store the thumbnail created by ImageMagick. string tmpImageThumbnailPath = Path.Combine(AppSetting.Instance.TempUploadDirectory, String.Concat(Guid.NewGuid().ToString(), ".jpg")); // Request that ImageMagick create the thumbnail. If successful, the file will be created. If not, it fails silently. ImageMagick.GenerateImage(GalleryObject.Original.FileNamePhysicalPath, tmpImageThumbnailPath, GalleryObject.GalleryId); if (File.Exists(tmpImageThumbnailPath)) { try { // ImageMagick successfully created a thumbnail image. Now resize it to the width and height we need. var size = ImageHelper.SaveImageFileAsJpeg(tmpImageThumbnailPath, newFilePath, gallerySetting.MaxThumbnailLength, false, gallerySetting.ThumbnailImageJpegQuality); GalleryObject.Thumbnail.Width = size.Width; GalleryObject.Thumbnail.Height = size.Height; //using (var originalBitmap = new Bitmap(tmpImageThumbnailPath)) //{ // var newSize = CalculateWidthAndHeight(new Size(originalBitmap.Width, originalBitmap.Height), gallerySetting.MaxThumbnailLength, false); // // Get JPEG quality value (0 - 100). This is ignored if imgFormat = GIF. // int jpegQuality = gallerySetting.ThumbnailImageJpegQuality; // // Generate the new image and save to disk. // var size = ImageHelper.SaveImageFile(originalBitmap, newFilePath, ImageFormat.Jpeg, newSize.Width, newSize.Height, jpegQuality); // GalleryObject.Thumbnail.Width = (int)size.Width; // GalleryObject.Thumbnail.Height = (int)size.Height; //} } catch (Exception ex) { ex.Data.Add("GSP Info", String.Format("This error occurred while trying to process the ImageMagick-generated file {0}. The original file is {1}. A generic thumbnail image will be created instead.", tmpImageThumbnailPath, GalleryObject.Original.FileNamePhysicalPath)); Events.EventController.RecordError(ex, AppSetting.Instance, GalleryObject.GalleryId, Factory.LoadGallerySettings()); // Default to a generic thumbnail image. GenerateGenericThumbnailImage(newFilePath, gallerySetting); } try { // Now delete the thumbnail image created by FFmpeg, but no worries if an error happens. The file is in the temp directory // which is cleaned out each time the app starts anyway. File.Delete(tmpImageThumbnailPath); } catch (IOException ex) { ex.Data.Add("GSP Info", "This error was handled and did not interfere with the user experience."); Events.EventController.RecordError(ex, AppSetting.Instance, GalleryObject.GalleryId, Factory.LoadGallerySettings()); } catch (UnauthorizedAccessException ex) { ex.Data.Add("GSP Info", "This error was handled and did not interfere with the user experience."); Events.EventController.RecordError(ex, AppSetting.Instance, GalleryObject.GalleryId, Factory.LoadGallerySettings()); } catch (NotSupportedException ex) { ex.Data.Add("GSP Info", "This error was handled and did not interfere with the user experience."); Events.EventController.RecordError(ex, AppSetting.Instance, GalleryObject.GalleryId, Factory.LoadGallerySettings()); } } else { // ImageMagick didn't create an image, so default to a generic one. GenerateGenericThumbnailImage(newFilePath, gallerySetting); } }