mirror of https://github.com/SixLabors/ImageSharp
25 changed files with 374 additions and 472 deletions
@ -0,0 +1,96 @@ |
|||||
|
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
// <copyright file="ImageHelpers.cs" company="James South">
|
||||
|
// Copyright (c) James South.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
// <summary>
|
||||
|
// The image helpers.
|
||||
|
// </summary>
|
||||
|
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
|
||||
|
namespace ImageProcessor.Web.Helpers |
||||
|
{ |
||||
|
#region Using
|
||||
|
using System.Drawing.Imaging; |
||||
|
using System.IO; |
||||
|
using System.Text.RegularExpressions; |
||||
|
#endregion
|
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The image helpers.
|
||||
|
/// </summary>
|
||||
|
public static class ImageHelpers |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The image format regex.
|
||||
|
/// </summary>
|
||||
|
private static readonly Regex FormatRegex = new Regex(@"(\.?)(j(pg|peg)|bmp|png|gif|ti(ff|f)|ico)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.RightToLeft); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The image format regex for matching the file format at the end of a string.
|
||||
|
/// </summary>
|
||||
|
private static readonly Regex EndFormatRegex = new Regex(@"(\.)(j(pg|peg)|bmp|png|gif|ti(ff|f)|ico)$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.RightToLeft); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Checks a given string to check whether the value contains a valid image extension.
|
||||
|
/// </summary>
|
||||
|
/// <param name="fileName">The string containing the filename to check.</param>
|
||||
|
/// <returns>True the value contains a valid image extension, otherwise false.</returns>
|
||||
|
public static bool IsValidImageExtension(string fileName) |
||||
|
{ |
||||
|
return EndFormatRegex.IsMatch(fileName); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct file extension for the given string input
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The string to parse.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct file extension for the given string input if it can find one; otherwise an empty string.
|
||||
|
/// </returns>
|
||||
|
public static string GetExtension(string input) |
||||
|
{ |
||||
|
Match match = FormatRegex.Matches(input)[0]; |
||||
|
|
||||
|
return match.Success ? match.Value : string.Empty; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct image format based on the given file extension.
|
||||
|
/// </summary>
|
||||
|
/// <param name="fileName">The string containing the filename to check against.</param>
|
||||
|
/// <returns>The correct image format based on the given filename.</returns>
|
||||
|
//public static ImageFormat GetImageFormat(string fileName)
|
||||
|
//{
|
||||
|
// string extension = Path.GetExtension(fileName);
|
||||
|
|
||||
|
// if (extension != null)
|
||||
|
// {
|
||||
|
// string ext = extension.ToUpperInvariant();
|
||||
|
|
||||
|
// switch (ext)
|
||||
|
// {
|
||||
|
// case ".ICO":
|
||||
|
// return ImageFormat.Icon;
|
||||
|
// case ".PNG":
|
||||
|
// return ImageFormat.Png;
|
||||
|
// case ".BMP":
|
||||
|
// return ImageFormat.Bmp;
|
||||
|
// case ".GIF":
|
||||
|
// return ImageFormat.Gif;
|
||||
|
// case ".TIF":
|
||||
|
// case ".TIFF":
|
||||
|
// return ImageFormat.Tiff;
|
||||
|
// default:
|
||||
|
// // Should be a jpeg.
|
||||
|
// return ImageFormat.Jpeg;
|
||||
|
// }
|
||||
|
// }
|
||||
|
|
||||
|
// // TODO: Show custom exception?
|
||||
|
// return null;
|
||||
|
//}
|
||||
|
} |
||||
|
} |
||||
@ -1,121 +0,0 @@ |
|||||
// --------------------------------------------------------------------------------------------------------------------
|
|
||||
// <copyright file="Preset.cs" company="James South">
|
|
||||
// Copyright (c) James South.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
// </copyright>
|
|
||||
// <summary>
|
|
||||
// Encapsulates methods to that allow the processing of preset image processing instructions.
|
|
||||
// </summary>
|
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
|
||||
|
|
||||
namespace ImageProcessor.Web |
|
||||
{ |
|
||||
using System.Collections.Generic; |
|
||||
using System.Drawing; |
|
||||
using System.Text.RegularExpressions; |
|
||||
using ImageProcessor.Processors; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Encapsulates methods to that allow the processing of preset image processing instructions.
|
|
||||
/// </summary>
|
|
||||
public class Preset : IGraphicsProcessor |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// The regular expression to search strings for.
|
|
||||
/// </summary>
|
|
||||
private static readonly Regex QueryRegex = new Regex(@"preset=[^&]*", RegexOptions.Compiled); |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the regular expression to search strings for.
|
|
||||
/// </summary>
|
|
||||
public Regex RegexPattern |
|
||||
{ |
|
||||
get |
|
||||
{ |
|
||||
return QueryRegex; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets DynamicParameter.
|
|
||||
/// </summary>
|
|
||||
public dynamic DynamicParameter |
|
||||
{ |
|
||||
get; |
|
||||
private set; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the order in which this processor is to be used in a chain.
|
|
||||
/// </summary>
|
|
||||
public int SortOrder |
|
||||
{ |
|
||||
get; |
|
||||
private set; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets or sets any additional settings required by the processor.
|
|
||||
/// </summary>
|
|
||||
public Dictionary<string, string> Settings |
|
||||
{ |
|
||||
get; |
|
||||
set; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// The match regex index.
|
|
||||
/// </summary>
|
|
||||
/// <param name="queryString">
|
|
||||
/// The query string.
|
|
||||
/// </param>
|
|
||||
/// <returns>
|
|
||||
/// The <see cref="int"/>.
|
|
||||
/// </returns>
|
|
||||
public int MatchRegexIndex(string queryString) |
|
||||
{ |
|
||||
int index = 0; |
|
||||
|
|
||||
// Set the sort order to max to allow filtering.
|
|
||||
this.SortOrder = int.MaxValue; |
|
||||
|
|
||||
foreach (Match match in this.RegexPattern.Matches(queryString)) |
|
||||
{ |
|
||||
if (match.Success) |
|
||||
{ |
|
||||
if (index == 0) |
|
||||
{ |
|
||||
// Set the index on the first instance only.
|
|
||||
this.SortOrder = match.Index; |
|
||||
string preset = match.Value; |
|
||||
this.DynamicParameter = preset; |
|
||||
} |
|
||||
|
|
||||
index += 1; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return this.SortOrder; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Processes the image.
|
|
||||
/// </summary>
|
|
||||
/// <param name="factory">The the current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class containing
|
|
||||
/// the image to process.</param>
|
|
||||
/// <returns>
|
|
||||
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class.
|
|
||||
/// </returns>
|
|
||||
public Image ProcessImage(ImageFactory factory) |
|
||||
{ |
|
||||
string preset = this.DynamicParameter; |
|
||||
string querystring; |
|
||||
this.Settings.TryGetValue(preset.Split('=')[1], out querystring); |
|
||||
|
|
||||
string oldQueryString = factory.QueryString; |
|
||||
string newQueryString = Regex.Replace(oldQueryString, preset, querystring ?? string.Empty); |
|
||||
|
|
||||
return factory.AddQueryString(newQueryString).AutoProcess().Image; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,149 @@ |
|||||
|
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
// <copyright file="ImageFormatExtensions.cs" company="James South">
|
||||
|
// Copyright (c) James South.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
// <summary>
|
||||
|
// Encapsulates a series of time saving extension methods to the <see cref="T:System.Drawing.Imaging.ImageFormat" />
|
||||
|
// class.
|
||||
|
// </summary>
|
||||
|
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
|
||||
|
namespace ImageProcessor.Extensions |
||||
|
{ |
||||
|
#region
|
||||
|
using System.Drawing.Imaging; |
||||
|
using System.Linq; |
||||
|
#endregion
|
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Encapsulates a series of time saving extension methods to the <see cref="T:System.Drawing.Imaging.ImageFormat" /> class.
|
||||
|
/// </summary>
|
||||
|
public static class ImageFormatExtensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Gets the correct mime-type for the given <see cref="T:System.Drawing.Imaging.ImageFormat" />.
|
||||
|
/// </summary>
|
||||
|
/// <param name="imageFormat">The <see cref="T:System.Drawing.Imaging.ImageFormat" />.</param>
|
||||
|
/// <returns>The correct mime-type for the given <see cref="T:System.Drawing.Imaging.ImageFormat" />.</returns>
|
||||
|
public static string GetMimeType(this ImageFormat imageFormat) |
||||
|
{ |
||||
|
if (imageFormat.Equals(ImageFormat.Icon)) |
||||
|
{ |
||||
|
return "image/x-icon"; |
||||
|
} |
||||
|
|
||||
|
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); |
||||
|
return codecs.First(codec => codec.FormatID == imageFormat.Guid).MimeType; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the name for the given <see cref="T:System.Drawing.Imaging.ImageFormat" />.
|
||||
|
/// </summary>
|
||||
|
/// <param name="format">
|
||||
|
/// The <see cref="T:System.Drawing.Imaging.ImageFormat" /> to get the name for.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The <see cref="string"/> representing the name of the <see cref="T:System.Drawing.Imaging.ImageFormat" />.
|
||||
|
/// </returns>
|
||||
|
public static string GetName(this ImageFormat format) |
||||
|
{ |
||||
|
if (format.Guid == ImageFormat.MemoryBmp.Guid) |
||||
|
{ |
||||
|
return "MemoryBMP"; |
||||
|
} |
||||
|
|
||||
|
if (format.Guid == ImageFormat.Bmp.Guid) |
||||
|
{ |
||||
|
return "Bmp"; |
||||
|
} |
||||
|
|
||||
|
if (format.Guid == ImageFormat.Emf.Guid) |
||||
|
{ |
||||
|
return "Emf"; |
||||
|
} |
||||
|
|
||||
|
if (format.Guid == ImageFormat.Wmf.Guid) |
||||
|
{ |
||||
|
return "Wmf"; |
||||
|
} |
||||
|
|
||||
|
if (format.Guid == ImageFormat.Gif.Guid) |
||||
|
{ |
||||
|
return "Gif"; |
||||
|
} |
||||
|
|
||||
|
if (format.Guid == ImageFormat.Jpeg.Guid) |
||||
|
{ |
||||
|
return "Jpeg"; |
||||
|
} |
||||
|
|
||||
|
if (format.Guid == ImageFormat.Png.Guid) |
||||
|
{ |
||||
|
return "Png"; |
||||
|
} |
||||
|
|
||||
|
if (format.Guid == ImageFormat.Tiff.Guid) |
||||
|
{ |
||||
|
return "Tiff"; |
||||
|
} |
||||
|
|
||||
|
if (format.Guid == ImageFormat.Exif.Guid) |
||||
|
{ |
||||
|
return "Exif"; |
||||
|
} |
||||
|
|
||||
|
if (format.Guid == ImageFormat.Icon.Guid) |
||||
|
{ |
||||
|
return "Icon"; |
||||
|
} |
||||
|
|
||||
|
return "[ImageFormat: " + format.Guid + "]"; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct file extension for the given <see cref="T:System.Drawing.Imaging.ImageFormat"/>.
|
||||
|
/// </summary>
|
||||
|
/// <param name="imageFormat">
|
||||
|
/// The <see cref="T:System.Drawing.Imaging.ImageFormat"/> to return the extension for.
|
||||
|
/// </param>
|
||||
|
/// <param name="originalExtension">
|
||||
|
/// The original Extension.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct file extension for the given <see cref="T:System.Drawing.Imaging.ImageFormat"/>.
|
||||
|
/// </returns>
|
||||
|
public static string GetFileExtension(this ImageFormat imageFormat, string originalExtension) |
||||
|
{ |
||||
|
string name = imageFormat.GetName(); |
||||
|
|
||||
|
switch (name) |
||||
|
{ |
||||
|
case "Icon": |
||||
|
return ".ico"; |
||||
|
case "Gif": |
||||
|
return ".gif"; |
||||
|
case "Bmp": |
||||
|
return ".bmp"; |
||||
|
case "Png": |
||||
|
return ".png"; |
||||
|
case "Tiff": |
||||
|
if (!string.IsNullOrWhiteSpace(originalExtension) && originalExtension.ToUpperInvariant() == ".TIF") |
||||
|
{ |
||||
|
return ".tif"; |
||||
|
} |
||||
|
|
||||
|
return ".tiff"; |
||||
|
case "Jpeg": |
||||
|
if (!string.IsNullOrWhiteSpace(originalExtension) && originalExtension.ToUpperInvariant() == ".JPG") |
||||
|
{ |
||||
|
return ".jpg"; |
||||
|
} |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,41 +0,0 @@ |
|||||
// -----------------------------------------------------------------------
|
|
||||
// <copyright file="EnumExtensions.cs" company="James South">
|
|
||||
// Copyright (c) James South.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
// </copyright>
|
|
||||
// -----------------------------------------------------------------------
|
|
||||
|
|
||||
namespace ImageProcessor.Helpers.Extensions |
|
||||
{ |
|
||||
#region Using
|
|
||||
using System; |
|
||||
using System.ComponentModel; |
|
||||
using System.Diagnostics.Contracts; |
|
||||
#endregion
|
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Encapsulates a series of time saving extension methods to <see cref="T:System.Enum">Enum</see>s.
|
|
||||
/// </summary>
|
|
||||
public static class EnumExtensions |
|
||||
{ |
|
||||
#region Methods
|
|
||||
/// <summary>
|
|
||||
/// Extends the <see cref="T:System.Enum">Enum</see> type to return the description attribute for the given type.
|
|
||||
/// Useful for when the type to match in the data source contains spaces.
|
|
||||
/// </summary>
|
|
||||
/// <param name="expression">The given <see cref="T:System.Enum">Enum</see> that this method extends.</param>
|
|
||||
/// <returns>A string containing the Enum's description attribute.</returns>
|
|
||||
public static string ToDescription(this Enum expression) |
|
||||
{ |
|
||||
Contract.Requires(expression != null); |
|
||||
|
|
||||
DescriptionAttribute[] descriptionAttribute = |
|
||||
(DescriptionAttribute[]) |
|
||||
expression.GetType().GetField(expression.ToString()) |
|
||||
.GetCustomAttributes(typeof(DescriptionAttribute), false); |
|
||||
|
|
||||
return descriptionAttribute.Length > 0 ? descriptionAttribute[0].Description : expression.ToString(); |
|
||||
} |
|
||||
#endregion
|
|
||||
} |
|
||||
} |
|
||||
@ -1,76 +0,0 @@ |
|||||
// -----------------------------------------------------------------------
|
|
||||
// <copyright file="ImageExtensions.cs" company="James South">
|
|
||||
// Copyright (c) James South.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
// </copyright>
|
|
||||
// -----------------------------------------------------------------------
|
|
||||
|
|
||||
namespace ImageProcessor.Helpers.Extensions |
|
||||
{ |
|
||||
#region Using
|
|
||||
|
|
||||
using System; |
|
||||
using System.Drawing; |
|
||||
using System.Drawing.Imaging; |
|
||||
using System.IO; |
|
||||
using System.Runtime.InteropServices; |
|
||||
|
|
||||
#endregion
|
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Extensions to the <see cref="T:System.Drawing.Image"/> class
|
|
||||
/// </summary>
|
|
||||
public static class ImageExtensions |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Converts an image to an array of bytes.
|
|
||||
/// </summary>
|
|
||||
/// <param name="image">The <see cref="T:System.Drawing.Image"/> instance that this method extends.</param>
|
|
||||
/// <param name="imageFormat">The <see cref="T:System.Drawing.Imaging.ImageFormat"/> to export the image with.</param>
|
|
||||
/// <returns>A byte array representing the current image.</returns>
|
|
||||
public static byte[] ToBytes(this Image image, ImageFormat imageFormat) |
|
||||
{ |
|
||||
BitmapData data = ((Bitmap)image).LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); |
|
||||
int length = image.Width * image.Height * 4; |
|
||||
byte[] byteArray = new byte[length]; |
|
||||
|
|
||||
if (data.Stride == image.Width * 4) |
|
||||
{ |
|
||||
Marshal.Copy(data.Scan0, byteArray, 0, length); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
for (int i = 0, l = image.Height; i < l; i++) |
|
||||
{ |
|
||||
IntPtr p = new IntPtr(data.Scan0.ToInt32() + data.Stride * i); |
|
||||
Marshal.Copy(p, byteArray, i * image.Width * 4, image.Width * 4); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
((Bitmap)image).UnlockBits(data); |
|
||||
|
|
||||
return byteArray; |
|
||||
} |
|
||||
|
|
||||
public static Image FromBytes(this Image image, byte[] bytes) |
|
||||
{ |
|
||||
BitmapData data = ((Bitmap)image).LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); |
|
||||
|
|
||||
if (data.Stride == image.Width * 4) |
|
||||
{ |
|
||||
Marshal.Copy(bytes, 0, data.Scan0, bytes.Length); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
for (int i = 0, l = image.Height; i < l; i++) |
|
||||
{ |
|
||||
IntPtr p = new IntPtr(data.Scan0.ToInt32() + data.Stride * i); |
|
||||
Marshal.Copy(bytes, i * image.Width * 4, p, image.Width * 4); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
((Bitmap)image).UnlockBits(data); |
|
||||
return image; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue