// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// Encapsulates useful image utility methods.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Imaging
{
#region Using
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
#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|ti(f|ff))", 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)
{
Match match = FormatRegex.Matches(request)[0];
switch (match.Value.ToUpperInvariant())
{
case "PNG":
case ".PNG":
return ResponseType.Png;
case "BMP":
case ".BMP":
return ResponseType.Bmp;
case "GIF":
case ".GIF":
return ResponseType.Gif;
case "TIF":
case "TIFF":
case ".TIF":
case ".TIFF":
return ResponseType.Tiff;
default:
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;
case ".TIF":
case ".TIFF":
return ImageFormat.Tiff;
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 original Extension.
///
///
/// The correct file extension for the given .
///
public static string GetExtensionFromImageFormat(ImageFormat imageFormat, string originalExtension)
{
switch (imageFormat.ToString())
{
case "Gif":
return ".gif";
case "Bmp":
return ".bmp";
case "Png":
return ".png";
case "Tif":
case "Tiff":
if (!string.IsNullOrWhiteSpace(originalExtension) && originalExtension.ToUpperInvariant() == ".TIFF")
{
return ".tiff";
}
return ".tif";
default:
if (!string.IsNullOrWhiteSpace(originalExtension) && originalExtension.ToUpperInvariant() == ".JPEG")
{
return ".jpeg";
}
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;
case ResponseType.Tiff:
return ImageFormat.Tiff;
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)
{
return FormatRegex.IsMatch(fileName);
}
///
/// Returns the correct file extension for the given string input
///
///
/// The string to parse.
///
///
/// The correct file extension for the given string input if it can find one; otherwise an empty string.
///
public static string GetExtension(string input)
{
Match match = FormatRegex.Matches(input)[0];
return match.Success ? match.Value : string.Empty;
}
/// Returns a value indicating whether or not the given bitmap is indexed.
/// The image to check
/// Whether or not the given bitmap is indexed.
public static bool IsIndexed(Image image)
{
// Test value of flags using bitwise AND.
return (image.PixelFormat & PixelFormat.Indexed) != 0;
}
}
}