Browse Source

Adding error handling

Former-commit-id: fa6d4684e2f3088ab1ed181ba11377a9d5431c82
pull/17/head
James South 12 years ago
parent
commit
a894fabf9d
  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.Drawing;
using System.Drawing.Imaging;
using ImageProcessor.Imaging;
/// <summary>

10
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)

16
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;
/// <summary>
/// The supported format base. Implement this class when building a supported format.
/// </summary>
public abstract class FormatBase : ISupportedImageFormat
{
/// <summary>
/// Initializes a new instance of the <see cref="FormatBase"/> class.
/// </summary>
protected FormatBase()
{
this.Quality = 90;
}
/// <summary>
/// Gets the file headers.
/// </summary>
@ -57,11 +64,6 @@ namespace ImageProcessor.Imaging.Formats
/// </summary>
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>
/// Gets or sets the quality of output for images.
/// </summary>
@ -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);
}
/// <summary>

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

@ -113,10 +113,7 @@ namespace ImageProcessor.Imaging.Formats
/// </returns>
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
/// </returns>
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);
}

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

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

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

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

19
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;
/// <summary>
/// Encapsulates methods to change the alpha component of the image to effect its transparency.
/// </summary>
@ -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)
{

9
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;
/// <summary>
/// Encapsulates methods to change the brightness component of the image.
/// </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)
{

9
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;
/// <summary>
/// Encapsulates methods to change the contrast component of the image.
/// </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)
{

10
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)
{

10
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)
{

12
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;
/// <summary>
/// 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)
{

2
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
/// <summary>
/// Sets the output of the image to a specific format.

9
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;
/// <summary>
@ -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)
{

9
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;
/// <summary>
@ -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)
{

8
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;
/// <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)
{

8
src/ImageProcessor/Processors/Rotate.cs

@ -15,6 +15,8 @@ namespace ImageProcessor.Processors
using System.Drawing;
using System.Drawing.Drawing2D;
using ImageProcessor.Core.Common.Exceptions;
/// <summary>
/// Encapsulates methods to rotate an image.
/// </summary>
@ -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)
{

11
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;
}
}
}

11
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
/// <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)
{

9
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;
/// <summary>
/// Tints an image with the given color.
/// </summary>
@ -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)
{

11
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;
/// <summary>
/// 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)
{

8
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;
/// <summary>
@ -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)
{

Loading…
Cancel
Save