namespace MyApp.Extensions
{
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
public enum ImagePlane
{
///
/// An enum constant representing the horizontal option.
///
Horizontal,
///
/// An enum constant representing the vertical option.
///
Vertical,
///
/// An enum constant representing the crop option.
///
Crop
}
public static class ImageExtensions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Crops the image.
/// Aaevans, 2/22/2016.
///
/// Thrown when one or more required arguments are null.
///
/// The source.
/// The dimension.
/// An Image.
////////////////////////////////////////////////////////////////////////////////////////////////////
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;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Resizes the image.
/// Aaevans, 2/22/2016.
///
/// Thrown when one or more required arguments are null.
///
/// The source image.
/// The dimension.
/// The ImagePlane.
/// An Image.
////////////////////////////////////////////////////////////////////////////////////////////////////
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;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Converts the color bitmap to gray scale.
/// Aaevans, 2/22/2016.
/// The bitmap.
/// A Bitmap.
////////////////////////////////////////////////////////////////////////////////////////////////////
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;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// A string extension method that converts an image object to a base64 string.
///
/// Aaevans, 2/22/2016.
/// The path to act on.
/// Describes the format to use.
/// The given data converted to a string.
////////////////////////////////////////////////////////////////////////////////////////////////////
public static string ToBase64(this string path, ImageFormat format)
{
var image = Image.FromFile(path);
return image.ToBase64(format);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// A string extension method that converts an image object to a base64 string.
///
/// Aaevans, 2/22/2016.
/// The image to act on.
/// Describes the format to use.
/// The given data converted to a string.
////////////////////////////////////////////////////////////////////////////////////////////////////
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;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// A string extension method that converts a base64 string to an image.
/// Aaevans, 2/22/2016.
/// The base64String to act on.
/// base64String as an Image.
////////////////////////////////////////////////////////////////////////////////////////////////////
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;
}
}
}