Advertisement
Willcode4cash

ImageExtensions

Sep 9th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.09 KB | None | 0 0
  1. namespace MyApp.Extensions
  2. {
  3.     using System;
  4.     using System.Drawing;
  5.     using System.Drawing.Drawing2D;
  6.     using System.Drawing.Imaging;
  7.     using System.IO;
  8.  
  9.     public enum ImagePlane
  10.     {
  11.         /// <summary>
  12.         /// An enum constant representing the horizontal option.
  13.         /// </summary>
  14.         Horizontal,
  15.  
  16.         /// <summary>
  17.         /// An enum constant representing the vertical option.
  18.         /// </summary>
  19.         Vertical,
  20.  
  21.         /// <summary>
  22.         /// An enum constant representing the crop option.
  23.         /// </summary>
  24.         Crop
  25.     }
  26.  
  27.     public static class ImageExtensions
  28.     {
  29.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  30.         /// <summary>   Crops the image. </summary>
  31.         /// <remarks>   Aaevans, 2/22/2016. </remarks>
  32.         /// <exception cref="ArgumentNullException">
  33.         ///     Thrown when one or more required arguments are null.
  34.         /// </exception>
  35.         /// <param name="source">       The source. </param>
  36.         /// <param name="dimension">    The dimension. </param>
  37.         /// <returns>   An Image. </returns>
  38.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  39.         public static Image CropImage(this Image source, int dimension)
  40.         {
  41.             if (source == null) throw new ArgumentNullException("source");
  42.  
  43.             try
  44.             {
  45.                 var resizedSource = source.ResizeImage(dimension, ImagePlane.Crop);
  46.                 var diffX = resizedSource.Width - dimension;
  47.                 var diffY = resizedSource.Height - dimension;
  48.                 var x = diffX > 1 ? diffX / 2 : 0;
  49.                 var y = diffY > 1 ? diffY / 2 : 0;
  50.                 var cropRectangle = new Rectangle(x, y, dimension, dimension);
  51.                 var sourceImage = new Bitmap(resizedSource);
  52.  
  53.                 Image croppedImage = sourceImage.Clone(cropRectangle, sourceImage.PixelFormat);
  54.                 return croppedImage;
  55.             }
  56.             catch (Exception)
  57.             {
  58.                 return null;
  59.             }
  60.         }
  61.  
  62.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  63.         /// <summary>   Resizes the image. </summary>
  64.         /// <remarks>   Aaevans, 2/22/2016. </remarks>
  65.         /// <exception cref="ArgumentNullException">
  66.         ///     Thrown when one or more required arguments are null.
  67.         /// </exception>
  68.         /// <param name="source">       The source image. </param>
  69.         /// <param name="dimension">    The dimension. </param>
  70.         /// <param name="plane">        The ImagePlane. </param>
  71.         /// <returns>   An Image. </returns>
  72.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  73.         public static Image ResizeImage(this Image source, int dimension, ImagePlane plane)
  74.         {
  75.             if (source == null) throw new ArgumentNullException("source");
  76.  
  77.             var destWidth = 1;
  78.             var destHeight = 1;
  79.  
  80.             try
  81.             {
  82.                 float factor;
  83.                 switch (plane)
  84.                 {
  85.                     case ImagePlane.Horizontal:
  86.                         factor = (dimension + 1) / (float)source.Width;
  87.                         destWidth = dimension;
  88.                         destHeight = (int)(source.Height * factor);
  89.                         break;
  90.  
  91.                     case ImagePlane.Vertical:
  92.                         factor = (dimension + 1) / (float)source.Height;
  93.                         destWidth = (int)(source.Width * factor);
  94.                         destHeight = dimension;
  95.                         break;
  96.  
  97.                     case ImagePlane.Crop:
  98.                         if (source.Height <= source.Width) factor = (dimension + 1) / (float)source.Height;
  99.                         else factor = (dimension + 1) / (float)source.Width;
  100.  
  101.                         destWidth = (int)(source.Width * factor);
  102.                         destHeight = (int)(source.Height * factor);
  103.                         break;
  104.                 }
  105.  
  106.                 var bmp = new Bitmap(destWidth, destHeight);
  107.  
  108.                 using (var gfx = Graphics.FromImage(bmp))
  109.                 {
  110.                     gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
  111.                     gfx.CompositingQuality = CompositingQuality.HighQuality;
  112.                     gfx.DrawImage(source, 0, 0, destWidth, destHeight);
  113.                 }
  114.  
  115.                 Image resizedImage = bmp;
  116.                 return resizedImage;
  117.             }
  118.             catch (Exception)
  119.             {
  120.                 return null;
  121.             }
  122.         }
  123.  
  124.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  125.         /// <summary>   Converts the color bitmap to gray scale. </summary>
  126.         /// <remarks>   Aaevans, 2/22/2016. </remarks>
  127.         /// <param name="bitmap">   The bitmap. </param>
  128.         /// <returns>   A Bitmap. </returns>
  129.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  130.         public static Bitmap AsGrayScaleImage(this Bitmap bitmap)
  131.         {
  132.             var newBitmap = new Bitmap(bitmap.Width, bitmap.Height);
  133.  
  134.             using (var g = Graphics.FromImage(newBitmap))
  135.             {
  136.                 var colorMatrix =
  137.                     new ColorMatrix(
  138.                         new[]
  139.                             {
  140.                                 new[] { .3f, .3f, .3f, 0, 0 },
  141.                                 new[] { .59f, .59f, .59f, 0, 0 },
  142.                                 new[] { .11f, .11f, .11f, 0, 0 },
  143.                                 new float[] { 0, 0, 0, 1, 0 },
  144.                                 new float[] { 0, 0, 0, 0, 1 }
  145.                             });
  146.  
  147.                 var attributes = new ImageAttributes();
  148.                 attributes.SetColorMatrix(colorMatrix);
  149.                 g.DrawImage(
  150.                     bitmap,
  151.                     new Rectangle(0, 0, bitmap.Width, bitmap.Height),
  152.                     0,
  153.                     0,
  154.                     bitmap.Width,
  155.                     bitmap.Height,
  156.                     GraphicsUnit.Pixel,
  157.                     attributes);
  158.             }
  159.  
  160.             return newBitmap;
  161.         }
  162.  
  163.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  164.         /// <summary>
  165.         ///     A string extension method that converts an image object to a base64 string.
  166.         /// </summary>
  167.         /// <remarks>   Aaevans, 2/22/2016. </remarks>
  168.         /// <param name="path">     The path to act on. </param>
  169.         /// <param name="format">   Describes the format to use. </param>
  170.         /// <returns>   The given data converted to a string. </returns>
  171.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  172.         public static string ToBase64(this string path, ImageFormat format)
  173.         {
  174.             var image = Image.FromFile(path);
  175.             return image.ToBase64(format);
  176.         }
  177.  
  178.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  179.         /// <summary>
  180.         ///     A string extension method that converts an image object to a base64 string.
  181.         /// </summary>
  182.         /// <remarks>   Aaevans, 2/22/2016. </remarks>
  183.         /// <param name="image">    The image to act on. </param>
  184.         /// <param name="format">   Describes the format to use. </param>
  185.         /// <returns>   The given data converted to a string. </returns>
  186.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  187.         public static string ToBase64(this Image image, ImageFormat format)
  188.         {
  189.             using (MemoryStream ms = new MemoryStream())
  190.             {
  191.                 image.Save(ms, format);
  192.                 byte[] imageBytes = ms.ToArray();
  193.  
  194.                 string base64String = Convert.ToBase64String(imageBytes);
  195.                 return base64String;
  196.             }
  197.         }
  198.  
  199.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  200.         /// <summary>   A string extension method that converts a base64 string to an image. </summary>
  201.         /// <remarks>   Aaevans, 2/22/2016. </remarks>
  202.         /// <param name="base64String"> The base64String to act on. </param>
  203.         /// <returns>   base64String as an Image. </returns>
  204.         ////////////////////////////////////////////////////////////////////////////////////////////////////
  205.         public static Image ToImage(this string base64String)
  206.         {
  207.             byte[] imageBytes = Convert.FromBase64String(base64String);
  208.             MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
  209.  
  210.             ms.Write(imageBytes, 0, imageBytes.Length);
  211.             Image image = Image.FromStream(ms, true);
  212.             return image;
  213.         }
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement