// -----------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
// -----------------------------------------------------------------------
namespace ImageProcessor.Imaging
{
#region Using
using System;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
#endregion
///
/// Encapsulates useful image utility methods.
///
public static class ImageUtils
{
///
/// The image format regex.
///
private static readonly Regex FormatRegex = new Regex(@"j(pg|peg)|bmp|png|gif", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
///
/// Returns the correct response type based on the given request path.
///
///
/// The request to match.
///
///
/// The correct .
///
public static ResponseType GetResponseType(string request)
{
foreach (Match match in FormatRegex.Matches(request))
{
switch (match.Value)
{
case "png":
return ResponseType.Png;
case "bmp":
return ResponseType.Bmp;
case "gif":
return ResponseType.Gif;
default:
return ResponseType.Jpeg;
}
}
return ResponseType.Jpeg;
}
///
/// Returns the correct image format based on the given file extension.
///
/// The string containing the filename to check against.
/// The correct image format based on the given filename.
public static ImageFormat GetImageFormat(string fileName)
{
string extension = Path.GetExtension(fileName);
if (extension != null)
{
string ext = extension.ToUpperInvariant();
switch (ext)
{
case ".PNG":
return ImageFormat.Png;
case ".BMP":
return ImageFormat.Bmp;
case ".GIF":
return ImageFormat.Gif;
default:
// Should be a jpeg.
return ImageFormat.Jpeg;
}
}
// TODO: Show custom exception??
return null;
}
///
/// Returns the correct file extension for the given .
///
///
/// The to return the extension for.
///
///
/// The correct file extension for the given .
///
public static string GetExtensionFromImageFormat(ImageFormat imageFormat)
{
switch (imageFormat.ToString())
{
case "Gif":
return ".gif";
case "Bmp":
return ".bmp";
case "Png":
return ".png";
default:
return ".jpg";
}
}
///
/// Returns the correct image format based on the given response type.
///
///
/// The to check against.
///
/// The correct image format based on the given response type.
public static ImageFormat GetImageFormat(ResponseType responseType)
{
switch (responseType)
{
case ResponseType.Png:
return ImageFormat.Png;
case ResponseType.Bmp:
return ImageFormat.Bmp;
case ResponseType.Gif:
return ImageFormat.Gif;
default:
// Should be a jpeg.
return ImageFormat.Jpeg;
}
}
///
/// Returns the first ImageCodeInfo instance with the specified mime type.
///
///
/// A string that contains the codec's Multipurpose Internet Mail Extensions (MIME) type.
///
///
/// The first ImageCodeInfo instance with the specified mime type.
///
public static ImageCodecInfo GetImageCodeInfo(string mimeType)
{
ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
return info.FirstOrDefault(ici => ici.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase));
}
///
/// Returns an instance of EncodingParameters for jpeg compression.
///
/// The quality to return the image at.
/// The encodingParameters for jpeg compression.
public static EncoderParameters GetEncodingParameters(int quality)
{
EncoderParameters encoderParameters = null;
try
{
// Create a series of encoder parameters.
encoderParameters = new EncoderParameters(1);
// Set the quality.
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
}
catch
{
if (encoderParameters != null)
{
encoderParameters.Dispose();
}
}
return encoderParameters;
}
///
/// Checks a given string to check whether the value contains a valid image extension.
///
/// The string containing the filename to check.
/// True the value contains a valid image extension, otherwise false.
public static bool IsValidImageExtension(string fileName)
{
bool isValid = false;
if (!string.IsNullOrWhiteSpace(fileName))
{
string[] fileExtensions = { ".BMP", ".JPG", ".PNG", ".GIF", ".JPEG" };
Parallel.ForEach(
fileExtensions,
(extension, loop) =>
{
if (fileName.ToUpperInvariant().EndsWith(extension))
{
isValid = true;
loop.Stop();
}
});
}
return isValid;
}
}
}