Browse Source

Adding error handling

Former-commit-id: 00bf32d7ac2150de3b2541266625743d0f18ecb3
pull/17/head
James South 12 years ago
parent
commit
bd747a3af8
  1. 1
      src/ImageProcessor/Core/Common/Extensions/ImageExtensions.cs
  2. 10
      src/ImageProcessor/ImageFactory.cs
  3. 16
      src/ImageProcessor/Imaging/Formats/FormatBase.cs
  4. 10
      src/ImageProcessor/Imaging/Formats/GifFormat.cs
  5. 5
      src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs
  6. 8
      src/ImageProcessor/Imaging/Formats/JpegFormat.cs
  7. 19
      src/ImageProcessor/Processors/Alpha.cs
  8. 9
      src/ImageProcessor/Processors/Brightness.cs
  9. 9
      src/ImageProcessor/Processors/Contrast.cs
  10. 10
      src/ImageProcessor/Processors/Crop.cs
  11. 10
      src/ImageProcessor/Processors/Filter.cs
  12. 12
      src/ImageProcessor/Processors/Flip.cs
  13. 2
      src/ImageProcessor/Processors/Format.cs
  14. 9
      src/ImageProcessor/Processors/GaussianBlur.cs
  15. 9
      src/ImageProcessor/Processors/GaussianSharpen.cs
  16. 8
      src/ImageProcessor/Processors/Resize.cs
  17. 8
      src/ImageProcessor/Processors/Rotate.cs
  18. 11
      src/ImageProcessor/Processors/RoundedCorners.cs
  19. 11
      src/ImageProcessor/Processors/Saturation.cs
  20. 9
      src/ImageProcessor/Processors/Tint.cs
  21. 11
      src/ImageProcessor/Processors/Vignette.cs
  22. 8
      src/ImageProcessor/Processors/Watermark.cs

1
src/ImageProcessor/Core/Common/Extensions/ImageExtensions.cs

@ -14,7 +14,6 @@ namespace ImageProcessor.Core.Common.Extensions
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using ImageProcessor.Imaging; using ImageProcessor.Imaging;
/// <summary> /// <summary>

10
src/ImageProcessor/ImageFactory.cs

@ -144,9 +144,6 @@ namespace ImageProcessor
throw new ImageFormatException("Input stream is not a supported format."); throw new ImageFormatException("Input stream is not a supported format.");
} }
this.backupFormat = format;
this.CurrentImageFormat = format;
// Set our image as the memory stream value. // Set our image as the memory stream value.
this.Image = format.Load(memoryStream); this.Image = format.Load(memoryStream);
@ -156,6 +153,8 @@ namespace ImageProcessor
// Set the other properties. // Set the other properties.
format.Quality = DefaultQuality; format.Quality = DefaultQuality;
format.IsIndexed = ImageUtils.IsIndexed(this.Image); format.IsIndexed = ImageUtils.IsIndexed(this.Image);
this.backupFormat = format;
this.CurrentImageFormat = format;
// Always load the data. // Always load the data.
foreach (PropertyItem propertyItem in this.Image.PropertyItems) foreach (PropertyItem propertyItem in this.Image.PropertyItems)
@ -192,9 +191,6 @@ namespace ImageProcessor
throw new ImageFormatException("Input stream is not a supported format."); throw new ImageFormatException("Input stream is not a supported format.");
} }
this.backupFormat = format;
this.CurrentImageFormat = format;
MemoryStream memoryStream = new MemoryStream(); MemoryStream memoryStream = new MemoryStream();
// Copy the stream. // Copy the stream.
@ -212,6 +208,8 @@ namespace ImageProcessor
// Set the other properties. // Set the other properties.
format.Quality = DefaultQuality; format.Quality = DefaultQuality;
format.IsIndexed = ImageUtils.IsIndexed(this.Image); format.IsIndexed = ImageUtils.IsIndexed(this.Image);
this.backupFormat = format;
this.CurrentImageFormat = format;
// Always load the data. // Always load the data.
foreach (PropertyItem propertyItem in this.Image.PropertyItems) foreach (PropertyItem propertyItem in this.Image.PropertyItems)

16
src/ImageProcessor/Imaging/Formats/FormatBase.cs

@ -14,13 +14,20 @@ namespace ImageProcessor.Imaging.Formats
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Linq;
/// <summary> /// <summary>
/// The supported format base. Implement this class when building a supported format. /// The supported format base. Implement this class when building a supported format.
/// </summary> /// </summary>
public abstract class FormatBase : ISupportedImageFormat public abstract class FormatBase : ISupportedImageFormat
{ {
/// <summary>
/// Initializes a new instance of the <see cref="FormatBase"/> class.
/// </summary>
protected FormatBase()
{
this.Quality = 90;
}
/// <summary> /// <summary>
/// Gets the file headers. /// Gets the file headers.
/// </summary> /// </summary>
@ -57,11 +64,6 @@ namespace ImageProcessor.Imaging.Formats
/// </summary> /// </summary>
public bool IsIndexed { get; set; } public bool IsIndexed { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the image format is animated.
/// </summary>
public bool IsAnimated { get; set; }
/// <summary> /// <summary>
/// Gets or sets the quality of output for images. /// Gets or sets the quality of output for images.
/// </summary> /// </summary>
@ -137,7 +139,7 @@ namespace ImageProcessor.Imaging.Formats
return false; return false;
} }
return this.MimeType.Equals(format.MimeType); return this.MimeType.Equals(format.MimeType) && this.IsIndexed.Equals(format.IsIndexed);
} }
/// <summary> /// <summary>

10
src/ImageProcessor/Imaging/Formats/GifFormat.cs

@ -113,10 +113,7 @@ namespace ImageProcessor.Imaging.Formats
/// </returns> /// </returns>
public override Image Save(Stream stream, Image image) public override Image Save(Stream stream, Image image)
{ {
// TODO: Move this in here. It doesn't need to be anywhere else. if (!ImageAnimator.CanAnimate(image))
ImageInfo imageInfo = image.GetImageInfo(this.ImageFormat, false);
if (!imageInfo.IsAnimated)
{ {
image = new OctreeQuantizer(255, 8).Quantize(image); image = new OctreeQuantizer(255, 8).Quantize(image);
} }
@ -135,10 +132,7 @@ namespace ImageProcessor.Imaging.Formats
/// </returns> /// </returns>
public override Image Save(string path, Image image) public override Image Save(string path, Image image)
{ {
// TODO: Move this in here. It doesn't need to be anywhere else. if (!ImageAnimator.CanAnimate(image))
ImageInfo imageInfo = image.GetImageInfo(this.ImageFormat, false);
if (!imageInfo.IsAnimated)
{ {
image = new OctreeQuantizer(255, 8).Quantize(image); image = new OctreeQuantizer(255, 8).Quantize(image);
} }

5
src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs

@ -50,11 +50,6 @@ namespace ImageProcessor.Imaging.Formats
/// </summary> /// </summary>
bool IsIndexed { get; set; } bool IsIndexed { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the image format is animated.
/// </summary>
bool IsAnimated { get; set; }
/// <summary> /// <summary>
/// Gets or sets the quality of output for images. /// Gets or sets the quality of output for images.
/// </summary> /// </summary>

8
src/ImageProcessor/Imaging/Formats/JpegFormat.cs

@ -22,14 +22,6 @@ namespace ImageProcessor.Imaging.Formats
/// </summary> /// </summary>
public sealed class JpegFormat : FormatBase public sealed class JpegFormat : FormatBase
{ {
/// <summary>
/// Initializes a new instance of the <see cref="JpegFormat"/> class.
/// </summary>
public JpegFormat()
{
this.Quality = 90;
}
/// <summary> /// <summary>
/// Gets the file headers. /// Gets the file headers.
/// </summary> /// </summary>

19
src/ImageProcessor/Processors/Alpha.cs

@ -10,10 +10,13 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using ImageProcessor.Core.Common.Exceptions;
/// <summary> /// <summary>
/// Encapsulates methods to change the alpha component of the image to effect its transparency. /// Encapsulates methods to change the alpha component of the image to effect its transparency.
/// </summary> /// </summary>
@ -76,14 +79,26 @@ namespace ImageProcessor.Processors
{ {
imageAttributes.SetColorMatrix(colorMatrix); 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.Dispose();
image = newImage; image = newImage;
} }
} }
} }
catch catch (Exception ex)
{
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
finally
{ {
if (newImage != null) if (newImage != null)
{ {

9
src/ImageProcessor/Processors/Brightness.cs

@ -10,10 +10,13 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using ImageProcessor.Core.Common.Exceptions;
/// <summary> /// <summary>
/// Encapsulates methods to change the brightness component of the image. /// Encapsulates methods to change the brightness component of the image.
/// </summary> /// </summary>
@ -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) if (newImage != null)
{ {

9
src/ImageProcessor/Processors/Contrast.cs

@ -10,10 +10,13 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using ImageProcessor.Core.Common.Exceptions;
/// <summary> /// <summary>
/// Encapsulates methods to change the contrast component of the image. /// Encapsulates methods to change the contrast component of the image.
/// </summary> /// </summary>
@ -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) if (newImage != null)
{ {

10
src/ImageProcessor/Processors/Crop.cs

@ -11,10 +11,14 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
#region Using #region Using
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using ImageProcessor.Core.Common.Exceptions;
using ImageProcessor.Imaging; using ImageProcessor.Imaging;
#endregion #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) if (newImage != null)
{ {

10
src/ImageProcessor/Processors/Filter.cs

@ -11,9 +11,13 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
#region Using #region Using
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using ImageProcessor.Core.Common.Exceptions;
using ImageProcessor.Imaging.Filters; using ImageProcessor.Imaging.Filters;
#endregion #endregion
@ -73,7 +77,11 @@ namespace ImageProcessor.Processors
return matrix.TransformImage(factory, image, newImage); 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) if (newImage != null)
{ {

12
src/ImageProcessor/Processors/Flip.cs

@ -10,11 +10,11 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
#region Using using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Text.RegularExpressions;
#endregion using ImageProcessor.Core.Common.Exceptions;
/// <summary> /// <summary>
/// Flips an image horizontally or vertically. /// Flips an image horizontally or vertically.
@ -74,7 +74,11 @@ namespace ImageProcessor.Processors
image.Dispose(); image.Dispose();
image = newImage; image = newImage;
} }
catch catch (Exception ex)
{
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
finally
{ {
if (newImage != null) if (newImage != null)
{ {

2
src/ImageProcessor/Processors/Format.cs

@ -10,11 +10,9 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
#region Using
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using ImageProcessor.Imaging.Formats; using ImageProcessor.Imaging.Formats;
#endregion
/// <summary> /// <summary>
/// Sets the output of the image to a specific format. /// Sets the output of the image to a specific format.

9
src/ImageProcessor/Processors/GaussianBlur.cs

@ -10,8 +10,11 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using ImageProcessor.Core.Common.Exceptions;
using ImageProcessor.Imaging; using ImageProcessor.Imaging;
/// <summary> /// <summary>
@ -62,7 +65,11 @@ namespace ImageProcessor.Processors
image.Dispose(); image.Dispose();
image = newImage; image = newImage;
} }
catch catch (Exception ex)
{
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
finally
{ {
if (newImage != null) if (newImage != null)
{ {

9
src/ImageProcessor/Processors/GaussianSharpen.cs

@ -10,8 +10,11 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using ImageProcessor.Core.Common.Exceptions;
using ImageProcessor.Imaging; using ImageProcessor.Imaging;
/// <summary> /// <summary>
@ -62,7 +65,11 @@ namespace ImageProcessor.Processors
image.Dispose(); image.Dispose();
image = newImage; image = newImage;
} }
catch catch (Exception ex)
{
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
finally
{ {
if (newImage != null) if (newImage != null)
{ {

8
src/ImageProcessor/Processors/Resize.cs

@ -17,6 +17,8 @@ namespace ImageProcessor.Processors
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using ImageProcessor.Core.Common.Exceptions;
using ImageProcessor.Imaging; using ImageProcessor.Imaging;
/// <summary> /// <summary>
@ -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) if (newImage != null)
{ {

8
src/ImageProcessor/Processors/Rotate.cs

@ -15,6 +15,8 @@ namespace ImageProcessor.Processors
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using ImageProcessor.Core.Common.Exceptions;
/// <summary> /// <summary>
/// Encapsulates methods to rotate an image. /// Encapsulates methods to rotate an image.
/// </summary> /// </summary>
@ -75,7 +77,11 @@ namespace ImageProcessor.Processors
image.Dispose(); image.Dispose();
image = newImage; image = newImage;
} }
catch catch (Exception ex)
{
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
finally
{ {
if (newImage != null) if (newImage != null)
{ {

11
src/ImageProcessor/Processors/RoundedCorners.cs

@ -11,9 +11,13 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
#region Using #region Using
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using ImageProcessor.Core.Common.Exceptions;
using ImageProcessor.Imaging; using ImageProcessor.Imaging;
#endregion #endregion
@ -79,7 +83,11 @@ namespace ImageProcessor.Processors
image.Dispose(); image.Dispose();
image = newImage; image = newImage;
} }
catch catch (Exception ex)
{
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
finally
{ {
if (newImage != null) if (newImage != null)
{ {
@ -177,6 +185,5 @@ namespace ImageProcessor.Processors
return newImage; return newImage;
} }
} }
} }

11
src/ImageProcessor/Processors/Saturation.cs

@ -11,9 +11,14 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
#region Using #region Using
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using ImageProcessor.Core.Common.Exceptions;
#endregion #endregion
/// <summary> /// <summary>
@ -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) if (newImage != null)
{ {

9
src/ImageProcessor/Processors/Tint.cs

@ -10,11 +10,14 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using ImageProcessor.Core.Common.Exceptions;
/// <summary> /// <summary>
/// Tints an image with the given color. /// Tints an image with the given color.
/// </summary> /// </summary>
@ -86,7 +89,11 @@ namespace ImageProcessor.Processors
return image; return image;
} }
catch catch (Exception ex)
{
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
finally
{ {
if (newImage != null) if (newImage != null)
{ {

11
src/ImageProcessor/Processors/Vignette.cs

@ -10,13 +10,12 @@
namespace ImageProcessor.Processors namespace ImageProcessor.Processors
{ {
#region Using
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;
#endregion using ImageProcessor.Core.Common.Exceptions;
/// <summary> /// <summary>
/// Encapsulates methods with which to add a vignette image effect to an image. /// 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) if (newImage != null)
{ {

8
src/ImageProcessor/Processors/Watermark.cs

@ -14,6 +14,8 @@ namespace ImageProcessor.Processors
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Text; using System.Drawing.Text;
using ImageProcessor.Core.Common.Exceptions;
using ImageProcessor.Imaging; using ImageProcessor.Imaging;
/// <summary> /// <summary>
@ -130,7 +132,11 @@ namespace ImageProcessor.Processors
image = newImage; image = newImage;
} }
} }
catch catch (Exception ex)
{
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
finally
{ {
if (newImage != null) if (newImage != null)
{ {

Loading…
Cancel
Save