예제 #1
0
        /// <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.ImageMagickConvertPath)))
            {
                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))
            {
                // The utility returns an empty string when it is successful, so something went wrong. Log it.
                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 output", convertOutput);

                Events.EventController.RecordError(ex, AppSetting.Instance, galleryId, Factory.LoadGallerySettings());
            }

            return convertOutput;
        }
예제 #2
0
        /// <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.ImageMagickConvertPath)))
            {
                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))
            {
                // The utility returns an empty string when it is successful, so something went wrong. Log it.
                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 output", convertOutput);

                Events.EventController.RecordError(ex, AppSetting.Instance, galleryId, Factory.LoadGallerySettings());
            }

            return(convertOutput);
        }
예제 #3
0
        /// <summary>
        /// Generates a thumbnail image for the media file at the specified <paramref name="mediaFilePath" /> and returns the output from the
        /// execution of the convert utility. The thumbnail is saved to
        /// <paramref name="thumbnailFilePath" />. 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="mediaFilePath">The full file path to the source media file. Example: D:\media\myfile.eps</param>
        /// <param name="thumbnailFilePath">The full file path to store the thumbnail 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 GenerateThumbnail(string mediaFilePath, string thumbnailFilePath, int galleryId)
        {
            string convertOutput = String.Empty;

            if ((AppSetting.Instance.AppTrustLevel != ApplicationTrustLevel.Full) || (String.IsNullOrEmpty(AppSetting.Instance.ImageMagickConvertPath)))
            {
                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"
            string args = string.Format(@"""{0}[0]"" ""{1}""", mediaFilePath, thumbnailFilePath);

            ImageMagick imageMagick = new ImageMagick();

            convertOutput = imageMagick.ExecuteConvert(args, galleryId);

            if (!String.IsNullOrEmpty(convertOutput))
            {
                // The utility returns an empty string when it is successful, so something went wrong. Log it.
                ErrorHandler.Error.Record(new BusinessException(convertOutput), galleryId, Factory.LoadGallerySettings(), AppSetting.Instance);
            }

            return(convertOutput);
        }
        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.GenerateThumbnail(this._galleryObject.Original.FileNamePhysicalPath, tmpImageThumbnailPath, this._galleryObject.GalleryId);

            if (File.Exists(tmpImageThumbnailPath))
            {
                int newWidth, newHeight;
                // ImageMagick successfully created a thumbnail image. Now resize it to the width and height we need.
                using (Bitmap originalBitmap = new Bitmap(tmpImageThumbnailPath))
                {
                    ImageHelper.CalculateThumbnailWidthAndHeight(originalBitmap.Width, originalBitmap.Height, out newWidth, out newHeight, false, gallerySetting.MaxThumbnailLength);

                    // Get JPEG quality value (0 - 100). This is ignored if imgFormat = GIF.
                    int jpegQuality = gallerySetting.ThumbnailImageJpegQuality;

                    // Generate the new image and save to disk.
                    ImageHelper.SaveImageFile(originalBitmap, newFilePath, ImageFormat.Jpeg, newWidth, newHeight, jpegQuality);
                }

                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)
                {
                    ErrorHandler.Error.Record(ex, this._galleryObject.GalleryId, Factory.LoadGallerySettings(), AppSetting.Instance);
                }
                catch (UnauthorizedAccessException ex)
                {
                    ErrorHandler.Error.Record(ex, this._galleryObject.GalleryId, Factory.LoadGallerySettings(), AppSetting.Instance);
                }
                catch (NotSupportedException ex)
                {
                    ErrorHandler.Error.Record(ex, this._galleryObject.GalleryId, Factory.LoadGallerySettings(), AppSetting.Instance);
                }

                this._galleryObject.Thumbnail.Width  = newWidth;
                this._galleryObject.Thumbnail.Height = newHeight;
            }
            else
            {
                // ImageMagick didn't create an image, so default to a generic one.
                GenerateGenericThumbnailImage(newFilePath, gallerySetting);
            }
        }
        private bool GenerateOptimizedImageUsingImageMagick(string newFilePath, IGallerySettings gallerySetting)
        {
            // Generate a temporary filename to store the thumbnail created by ImageMagick.
            string tmpImageOptimizedPath = 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.
                tmpImageOptimizedPath = _galleryObject.Original.TempFilePath;
            }

            // Request that ImageMagick create the thumbnail. If successful, the file will be created. If not, it fails silently.
            if (!File.Exists(tmpImageOptimizedPath))
            {
                ImageMagick.GenerateThumbnail(this._galleryObject.Original.FileNamePhysicalPath, tmpImageOptimizedPath, this._galleryObject.GalleryId);
            }

            if (File.Exists(tmpImageOptimizedPath))
            {
                int newWidth;
                int newHeight;
                // ImageMagick successfully created a thumbnail image. Now resize it to the width and height we need.
                using (Bitmap originalBitmap = new Bitmap(tmpImageOptimizedPath))
                {
                    ImageHelper.CalculateOptimizedWidthAndHeight(originalBitmap, out newWidth, out newHeight, _galleryObject.GalleryId);

                    // Get JPEG quality value (0 - 100). This is ignored if imgFormat = GIF.
                    int jpegQuality = gallerySetting.OptimizedImageJpegQuality;

                    // Generate the new image and save to disk.
                    ImageHelper.SaveImageFile(originalBitmap, newFilePath, ImageFormat.Jpeg, newWidth, newHeight, jpegQuality);
                }

                this._galleryObject.Optimized.Width  = newWidth;
                this._galleryObject.Optimized.Height = newHeight;

                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #6
0
        protected Size 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));
                }
                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);
        }
예제 #7
0
        /// <summary>
        /// Generates a thumbnail image for the media file at the specified <paramref name="mediaFilePath" /> and returns the output from the
        /// execution of the convert utility. The thumbnail is saved to 
        /// <paramref name="thumbnailFilePath" />. 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="mediaFilePath">The full file path to the source media file. Example: D:\media\myfile.eps</param>
        /// <param name="thumbnailFilePath">The full file path to store the thumbnail 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 GenerateThumbnail(string mediaFilePath, string thumbnailFilePath, int galleryId)
        {
            string convertOutput = String.Empty;

            if ((AppSetting.Instance.AppTrustLevel != ApplicationTrustLevel.Full) || (String.IsNullOrEmpty(AppSetting.Instance.ImageMagickConvertPath)))
            {
                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"
            string args = String.Format(CultureInfo.InvariantCulture, @"""{0}[0]"" ""{1}""", mediaFilePath, thumbnailFilePath);

            ImageMagick imageMagick = new ImageMagick();
            convertOutput = imageMagick.ExecuteConvert(args, galleryId);

            if (!String.IsNullOrEmpty(convertOutput))
            {
                // The utility returns an empty string when it is successful, so something went wrong. Log it.
                ErrorHandler.Error.Record(new BusinessException(convertOutput), galleryId, Factory.LoadGallerySettings(), AppSetting.Instance);
            }

            return convertOutput;
        }
예제 #8
0
        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.
                    using (var originalBitmap = new Bitmap(tmpImageThumbnailPath))
                    {
                        var newSize = CalculateWidthAndHeight(new System.Windows.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);
            }
        }