namespace MyApp.Extensions
{
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
public enum ImagePlane
{
/// <summary>
/// An enum constant representing the horizontal option.
/// </summary>
Horizontal,
/// <summary>
/// An enum constant representing the vertical option.
/// </summary>
Vertical,
/// <summary>
/// An enum constant representing the crop option.
/// </summary>
Crop
}
public static class ImageExtensions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Crops the image. </summary>
/// <remarks> Aaevans, 2/22/2016. </remarks>
/// <exception cref="ArgumentNullException">
/// Thrown when one or more required arguments are null.
/// </exception>
/// <param name="source"> The source. </param>
/// <param name="dimension"> The dimension. </param>
/// <returns> An Image. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public static Image CropImage(this Image source, int dimension)
{
if (source == null) throw new ArgumentNullException("source");
try
{
var resizedSource = source.ResizeImage(dimension, ImagePlane.Crop);
var diffX = resizedSource.Width - dimension;
var diffY = resizedSource.Height - dimension;
var x = diffX > 1 ? diffX / 2 : 0;
var y = diffY > 1 ? diffY / 2 : 0;
var cropRectangle = new Rectangle(x, y, dimension, dimension);
var sourceImage = new Bitmap(resizedSource);
Image croppedImage = sourceImage.Clone(cropRectangle, sourceImage.PixelFormat);
return croppedImage;
}
catch (Exception)
{
return null;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Resizes the image. </summary>
/// <remarks> Aaevans, 2/22/2016. </remarks>
/// <exception cref="ArgumentNullException">
/// Thrown when one or more required arguments are null.
/// </exception>
/// <param name="source"> The source image. </param>
/// <param name="dimension"> The dimension. </param>
/// <param name="plane"> The ImagePlane. </param>
/// <returns> An Image. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public static Image ResizeImage(this Image source, int dimension, ImagePlane plane)
{
if (source == null) throw new ArgumentNullException("source");
var destWidth = 1;
var destHeight = 1;
try
{
float factor;
switch (plane)
{
case ImagePlane.Horizontal:
factor = (dimension + 1) / (float)source.Width;
destWidth = dimension;
destHeight = (int)(source.Height * factor);
break;
case ImagePlane.Vertical:
factor = (dimension + 1) / (float)source.Height;
destWidth = (int)(source.Width * factor);
destHeight = dimension;
break;
case ImagePlane.Crop:
if (source.Height <= source.Width) factor = (dimension + 1) / (float)source.Height;
else factor = (dimension + 1) / (float)source.Width;
destWidth = (int)(source.Width * factor);
destHeight = (int)(source.Height * factor);
break;
}
var bmp = new Bitmap(destWidth, destHeight);
using (var gfx = Graphics.FromImage(bmp))
{
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.CompositingQuality = CompositingQuality.HighQuality;
gfx.DrawImage(source, 0, 0, destWidth, destHeight);
}
Image resizedImage = bmp;
return resizedImage;
}
catch (Exception)
{
return null;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Converts the color bitmap to gray scale. </summary>
/// <remarks> Aaevans, 2/22/2016. </remarks>
/// <param name="bitmap"> The bitmap. </param>
/// <returns> A Bitmap. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public static Bitmap AsGrayScaleImage(this Bitmap bitmap)
{
var newBitmap = new Bitmap(bitmap.Width, bitmap.Height);
using (var g = Graphics.FromImage(newBitmap))
{
var colorMatrix =
new ColorMatrix(
new[]
{
new[] { .3f, .3f, .3f, 0, 0 },
new[] { .59f, .59f, .59f, 0, 0 },
new[] { .11f, .11f, .11f, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 0, 0, 0, 0, 1 }
});
var attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(
bitmap,
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
0,
0,
bitmap.Width,
bitmap.Height,
GraphicsUnit.Pixel,
attributes);
}
return newBitmap;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// A string extension method that converts an image object to a base64 string.
/// </summary>
/// <remarks> Aaevans, 2/22/2016. </remarks>
/// <param name="path"> The path to act on. </param>
/// <param name="format"> Describes the format to use. </param>
/// <returns> The given data converted to a string. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public static string ToBase64(this string path, ImageFormat format)
{
var image = Image.FromFile(path);
return image.ToBase64(format);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// A string extension method that converts an image object to a base64 string.
/// </summary>
/// <remarks> Aaevans, 2/22/2016. </remarks>
/// <param name="image"> The image to act on. </param>
/// <param name="format"> Describes the format to use. </param>
/// <returns> The given data converted to a string. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public static string ToBase64(this Image image, ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> A string extension method that converts a base64 string to an image. </summary>
/// <remarks> Aaevans, 2/22/2016. </remarks>
/// <param name="base64String"> The base64String to act on. </param>
/// <returns> base64String as an Image. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public static Image ToImage(this string base64String)
{
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
}
}
Comments