From bd747a3af8c97624d9e79f2229201bb0fcd223ad Mon Sep 17 00:00:00 2001 From: James South Date: Sun, 22 Jun 2014 20:55:55 +0100 Subject: [PATCH] Adding error handling Former-commit-id: 00bf32d7ac2150de3b2541266625743d0f18ecb3 --- .../Core/Common/Extensions/ImageExtensions.cs | 1 - src/ImageProcessor/ImageFactory.cs | 10 ++++------ .../Imaging/Formats/FormatBase.cs | 16 +++++++++------- .../Imaging/Formats/GifFormat.cs | 10 ++-------- .../Imaging/Formats/ISupportedImageFormat.cs | 5 ----- .../Imaging/Formats/JpegFormat.cs | 8 -------- src/ImageProcessor/Processors/Alpha.cs | 19 +++++++++++++++++-- src/ImageProcessor/Processors/Brightness.cs | 9 ++++++++- src/ImageProcessor/Processors/Contrast.cs | 9 ++++++++- src/ImageProcessor/Processors/Crop.cs | 10 +++++++++- src/ImageProcessor/Processors/Filter.cs | 10 +++++++++- src/ImageProcessor/Processors/Flip.cs | 12 ++++++++---- src/ImageProcessor/Processors/Format.cs | 2 -- src/ImageProcessor/Processors/GaussianBlur.cs | 9 ++++++++- .../Processors/GaussianSharpen.cs | 9 ++++++++- src/ImageProcessor/Processors/Resize.cs | 8 +++++++- src/ImageProcessor/Processors/Rotate.cs | 8 +++++++- .../Processors/RoundedCorners.cs | 11 +++++++++-- src/ImageProcessor/Processors/Saturation.cs | 11 ++++++++++- src/ImageProcessor/Processors/Tint.cs | 9 ++++++++- src/ImageProcessor/Processors/Vignette.cs | 11 +++++++---- src/ImageProcessor/Processors/Watermark.cs | 8 +++++++- 22 files changed, 145 insertions(+), 60 deletions(-) diff --git a/src/ImageProcessor/Core/Common/Extensions/ImageExtensions.cs b/src/ImageProcessor/Core/Common/Extensions/ImageExtensions.cs index cff34a9f82..39210c33cc 100644 --- a/src/ImageProcessor/Core/Common/Extensions/ImageExtensions.cs +++ b/src/ImageProcessor/Core/Common/Extensions/ImageExtensions.cs @@ -14,7 +14,6 @@ namespace ImageProcessor.Core.Common.Extensions using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; - using ImageProcessor.Imaging; /// diff --git a/src/ImageProcessor/ImageFactory.cs b/src/ImageProcessor/ImageFactory.cs index 9a928e5411..65de7464b6 100644 --- a/src/ImageProcessor/ImageFactory.cs +++ b/src/ImageProcessor/ImageFactory.cs @@ -144,9 +144,6 @@ namespace ImageProcessor throw new ImageFormatException("Input stream is not a supported format."); } - this.backupFormat = format; - this.CurrentImageFormat = format; - // Set our image as the memory stream value. this.Image = format.Load(memoryStream); @@ -156,6 +153,8 @@ namespace ImageProcessor // Set the other properties. format.Quality = DefaultQuality; format.IsIndexed = ImageUtils.IsIndexed(this.Image); + this.backupFormat = format; + this.CurrentImageFormat = format; // Always load the data. foreach (PropertyItem propertyItem in this.Image.PropertyItems) @@ -192,9 +191,6 @@ namespace ImageProcessor throw new ImageFormatException("Input stream is not a supported format."); } - this.backupFormat = format; - this.CurrentImageFormat = format; - MemoryStream memoryStream = new MemoryStream(); // Copy the stream. @@ -212,6 +208,8 @@ namespace ImageProcessor // Set the other properties. format.Quality = DefaultQuality; format.IsIndexed = ImageUtils.IsIndexed(this.Image); + this.backupFormat = format; + this.CurrentImageFormat = format; // Always load the data. foreach (PropertyItem propertyItem in this.Image.PropertyItems) diff --git a/src/ImageProcessor/Imaging/Formats/FormatBase.cs b/src/ImageProcessor/Imaging/Formats/FormatBase.cs index 182ddde3b3..ea00e82a26 100644 --- a/src/ImageProcessor/Imaging/Formats/FormatBase.cs +++ b/src/ImageProcessor/Imaging/Formats/FormatBase.cs @@ -14,13 +14,20 @@ namespace ImageProcessor.Imaging.Formats using System.Drawing; using System.Drawing.Imaging; using System.IO; - using System.Linq; /// /// The supported format base. Implement this class when building a supported format. /// public abstract class FormatBase : ISupportedImageFormat { + /// + /// Initializes a new instance of the class. + /// + protected FormatBase() + { + this.Quality = 90; + } + /// /// Gets the file headers. /// @@ -57,11 +64,6 @@ namespace ImageProcessor.Imaging.Formats /// public bool IsIndexed { get; set; } - /// - /// Gets or sets a value indicating whether the image format is animated. - /// - public bool IsAnimated { get; set; } - /// /// Gets or sets the quality of output for images. /// @@ -137,7 +139,7 @@ namespace ImageProcessor.Imaging.Formats return false; } - return this.MimeType.Equals(format.MimeType); + return this.MimeType.Equals(format.MimeType) && this.IsIndexed.Equals(format.IsIndexed); } /// diff --git a/src/ImageProcessor/Imaging/Formats/GifFormat.cs b/src/ImageProcessor/Imaging/Formats/GifFormat.cs index 4a88b6b6f7..f29e3f446e 100644 --- a/src/ImageProcessor/Imaging/Formats/GifFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/GifFormat.cs @@ -113,10 +113,7 @@ namespace ImageProcessor.Imaging.Formats /// public override Image Save(Stream stream, Image image) { - // TODO: Move this in here. It doesn't need to be anywhere else. - ImageInfo imageInfo = image.GetImageInfo(this.ImageFormat, false); - - if (!imageInfo.IsAnimated) + if (!ImageAnimator.CanAnimate(image)) { image = new OctreeQuantizer(255, 8).Quantize(image); } @@ -135,10 +132,7 @@ namespace ImageProcessor.Imaging.Formats /// public override Image Save(string path, Image image) { - // TODO: Move this in here. It doesn't need to be anywhere else. - ImageInfo imageInfo = image.GetImageInfo(this.ImageFormat, false); - - if (!imageInfo.IsAnimated) + if (!ImageAnimator.CanAnimate(image)) { image = new OctreeQuantizer(255, 8).Quantize(image); } diff --git a/src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs b/src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs index 185cb2e8ea..05e69260af 100644 --- a/src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs @@ -50,11 +50,6 @@ namespace ImageProcessor.Imaging.Formats /// bool IsIndexed { get; set; } - /// - /// Gets or sets a value indicating whether the image format is animated. - /// - bool IsAnimated { get; set; } - /// /// Gets or sets the quality of output for images. /// diff --git a/src/ImageProcessor/Imaging/Formats/JpegFormat.cs b/src/ImageProcessor/Imaging/Formats/JpegFormat.cs index ca2a3f546b..9447375d97 100644 --- a/src/ImageProcessor/Imaging/Formats/JpegFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/JpegFormat.cs @@ -22,14 +22,6 @@ namespace ImageProcessor.Imaging.Formats /// public sealed class JpegFormat : FormatBase { - /// - /// Initializes a new instance of the class. - /// - public JpegFormat() - { - this.Quality = 90; - } - /// /// Gets the file headers. /// diff --git a/src/ImageProcessor/Processors/Alpha.cs b/src/ImageProcessor/Processors/Alpha.cs index cdd505f84d..4d98e8b378 100644 --- a/src/ImageProcessor/Processors/Alpha.cs +++ b/src/ImageProcessor/Processors/Alpha.cs @@ -10,10 +10,13 @@ namespace ImageProcessor.Processors { + using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; + using ImageProcessor.Core.Common.Exceptions; + /// /// Encapsulates methods to change the alpha component of the image to effect its transparency. /// @@ -76,14 +79,26 @@ namespace ImageProcessor.Processors { imageAttributes.SetColorMatrix(colorMatrix); - graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); + graphics.DrawImage( + image, + new Rectangle(0, 0, image.Width, image.Height), + 0, + 0, + image.Width, + image.Height, + GraphicsUnit.Pixel, + imageAttributes); image.Dispose(); image = newImage; } } } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/Brightness.cs b/src/ImageProcessor/Processors/Brightness.cs index e711d5c191..0e3fed7176 100644 --- a/src/ImageProcessor/Processors/Brightness.cs +++ b/src/ImageProcessor/Processors/Brightness.cs @@ -10,10 +10,13 @@ namespace ImageProcessor.Processors { + using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; + using ImageProcessor.Core.Common.Exceptions; + /// /// Encapsulates methods to change the brightness component of the image. /// @@ -96,7 +99,11 @@ namespace ImageProcessor.Processors } } } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/Contrast.cs b/src/ImageProcessor/Processors/Contrast.cs index dbe6b91c34..f1ceb79ed4 100644 --- a/src/ImageProcessor/Processors/Contrast.cs +++ b/src/ImageProcessor/Processors/Contrast.cs @@ -10,10 +10,13 @@ namespace ImageProcessor.Processors { + using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; + using ImageProcessor.Core.Common.Exceptions; + /// /// Encapsulates methods to change the contrast component of the image. /// @@ -93,7 +96,11 @@ namespace ImageProcessor.Processors } } } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/Crop.cs b/src/ImageProcessor/Processors/Crop.cs index 6a3cb03c0b..85bb9886c9 100644 --- a/src/ImageProcessor/Processors/Crop.cs +++ b/src/ImageProcessor/Processors/Crop.cs @@ -11,10 +11,14 @@ namespace ImageProcessor.Processors { #region Using + + using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; + + using ImageProcessor.Core.Common.Exceptions; using ImageProcessor.Imaging; #endregion @@ -132,7 +136,11 @@ namespace ImageProcessor.Processors } } } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/Filter.cs b/src/ImageProcessor/Processors/Filter.cs index 6e3eb84595..ecd6700f16 100644 --- a/src/ImageProcessor/Processors/Filter.cs +++ b/src/ImageProcessor/Processors/Filter.cs @@ -11,9 +11,13 @@ namespace ImageProcessor.Processors { #region Using + + using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; + + using ImageProcessor.Core.Common.Exceptions; using ImageProcessor.Imaging.Filters; #endregion @@ -73,7 +77,11 @@ namespace ImageProcessor.Processors return matrix.TransformImage(factory, image, newImage); } } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/Flip.cs b/src/ImageProcessor/Processors/Flip.cs index 0b67ee04b9..a93d330f36 100644 --- a/src/ImageProcessor/Processors/Flip.cs +++ b/src/ImageProcessor/Processors/Flip.cs @@ -10,11 +10,11 @@ namespace ImageProcessor.Processors { - #region Using + using System; using System.Collections.Generic; using System.Drawing; - using System.Text.RegularExpressions; - #endregion + + using ImageProcessor.Core.Common.Exceptions; /// /// Flips an image horizontally or vertically. @@ -74,7 +74,11 @@ namespace ImageProcessor.Processors image.Dispose(); image = newImage; } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/Format.cs b/src/ImageProcessor/Processors/Format.cs index 7a2c9ee688..921b5c939e 100644 --- a/src/ImageProcessor/Processors/Format.cs +++ b/src/ImageProcessor/Processors/Format.cs @@ -10,11 +10,9 @@ namespace ImageProcessor.Processors { - #region Using using System.Collections.Generic; using System.Drawing; using ImageProcessor.Imaging.Formats; - #endregion /// /// Sets the output of the image to a specific format. diff --git a/src/ImageProcessor/Processors/GaussianBlur.cs b/src/ImageProcessor/Processors/GaussianBlur.cs index fa8bbb65ed..60abedec1f 100644 --- a/src/ImageProcessor/Processors/GaussianBlur.cs +++ b/src/ImageProcessor/Processors/GaussianBlur.cs @@ -10,8 +10,11 @@ namespace ImageProcessor.Processors { + using System; using System.Collections.Generic; using System.Drawing; + + using ImageProcessor.Core.Common.Exceptions; using ImageProcessor.Imaging; /// @@ -62,7 +65,11 @@ namespace ImageProcessor.Processors image.Dispose(); image = newImage; } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/GaussianSharpen.cs b/src/ImageProcessor/Processors/GaussianSharpen.cs index c7ea7a4ef7..94c6973627 100644 --- a/src/ImageProcessor/Processors/GaussianSharpen.cs +++ b/src/ImageProcessor/Processors/GaussianSharpen.cs @@ -10,8 +10,11 @@ namespace ImageProcessor.Processors { + using System; using System.Collections.Generic; using System.Drawing; + + using ImageProcessor.Core.Common.Exceptions; using ImageProcessor.Imaging; /// @@ -62,7 +65,11 @@ namespace ImageProcessor.Processors image.Dispose(); image = newImage; } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/Resize.cs b/src/ImageProcessor/Processors/Resize.cs index 1183068c6d..cce1177e2f 100644 --- a/src/ImageProcessor/Processors/Resize.cs +++ b/src/ImageProcessor/Processors/Resize.cs @@ -17,6 +17,8 @@ namespace ImageProcessor.Processors using System.Drawing.Imaging; using System.Globalization; using System.Linq; + + using ImageProcessor.Core.Common.Exceptions; using ImageProcessor.Imaging; /// @@ -359,7 +361,11 @@ namespace ImageProcessor.Processors } } } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/Rotate.cs b/src/ImageProcessor/Processors/Rotate.cs index e9cc4d8388..ccf23514a5 100644 --- a/src/ImageProcessor/Processors/Rotate.cs +++ b/src/ImageProcessor/Processors/Rotate.cs @@ -15,6 +15,8 @@ namespace ImageProcessor.Processors using System.Drawing; using System.Drawing.Drawing2D; + using ImageProcessor.Core.Common.Exceptions; + /// /// Encapsulates methods to rotate an image. /// @@ -75,7 +77,11 @@ namespace ImageProcessor.Processors image.Dispose(); image = newImage; } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/RoundedCorners.cs b/src/ImageProcessor/Processors/RoundedCorners.cs index a67fbdb16d..89e4b683ca 100644 --- a/src/ImageProcessor/Processors/RoundedCorners.cs +++ b/src/ImageProcessor/Processors/RoundedCorners.cs @@ -11,9 +11,13 @@ namespace ImageProcessor.Processors { #region Using + + using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; + + using ImageProcessor.Core.Common.Exceptions; using ImageProcessor.Imaging; #endregion @@ -79,7 +83,11 @@ namespace ImageProcessor.Processors image.Dispose(); image = newImage; } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { @@ -177,6 +185,5 @@ namespace ImageProcessor.Processors return newImage; } - } } diff --git a/src/ImageProcessor/Processors/Saturation.cs b/src/ImageProcessor/Processors/Saturation.cs index 7c170030e0..09fae902cf 100644 --- a/src/ImageProcessor/Processors/Saturation.cs +++ b/src/ImageProcessor/Processors/Saturation.cs @@ -11,9 +11,14 @@ namespace ImageProcessor.Processors { #region Using + + using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; + + using ImageProcessor.Core.Common.Exceptions; + #endregion /// @@ -126,7 +131,11 @@ namespace ImageProcessor.Processors } } } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/Tint.cs b/src/ImageProcessor/Processors/Tint.cs index 66843e8a17..939cecb9c5 100644 --- a/src/ImageProcessor/Processors/Tint.cs +++ b/src/ImageProcessor/Processors/Tint.cs @@ -10,11 +10,14 @@ namespace ImageProcessor.Processors { + using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; + using ImageProcessor.Core.Common.Exceptions; + /// /// Tints an image with the given color. /// @@ -86,7 +89,11 @@ namespace ImageProcessor.Processors return image; } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/Vignette.cs b/src/ImageProcessor/Processors/Vignette.cs index 9c18cfae28..f600f35edb 100644 --- a/src/ImageProcessor/Processors/Vignette.cs +++ b/src/ImageProcessor/Processors/Vignette.cs @@ -10,13 +10,12 @@ namespace ImageProcessor.Processors { - #region Using using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; - using System.Text.RegularExpressions; - #endregion + + using ImageProcessor.Core.Common.Exceptions; /// /// Encapsulates methods with which to add a vignette image effect to an image. @@ -117,7 +116,11 @@ namespace ImageProcessor.Processors } } } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) { diff --git a/src/ImageProcessor/Processors/Watermark.cs b/src/ImageProcessor/Processors/Watermark.cs index 241b5efbd9..b0ff1e00d1 100644 --- a/src/ImageProcessor/Processors/Watermark.cs +++ b/src/ImageProcessor/Processors/Watermark.cs @@ -14,6 +14,8 @@ namespace ImageProcessor.Processors using System.Collections.Generic; using System.Drawing; using System.Drawing.Text; + + using ImageProcessor.Core.Common.Exceptions; using ImageProcessor.Imaging; /// @@ -130,7 +132,11 @@ namespace ImageProcessor.Processors image = newImage; } } - catch + catch (Exception ex) + { + throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); + } + finally { if (newImage != null) {