Advertisement
EmilySamantha80

Image manipulation

Dec 8th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.15 KB | None | 0 0
  1. // Title:  Image manipulation
  2. // Author: Emily Heiner
  3. // Date:   2016-12-08
  4. // This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported license.
  5. // You are free to share and adapt this code for any purposes, commerical and non-commercial,
  6. // as long as appropriate credit is given, you indicate if changes were made, and you share your
  7. // modified code. License details can be found at https://creativecommons.org/licenses/by-sa/3.0/
  8.  
  9. using System;
  10. using System.IO;
  11. using System.Drawing.Drawing2D;
  12. using System.Drawing.Imaging;
  13. using System.Drawing;
  14.  
  15. namespace ESH.Utility
  16. {
  17.     public static class ImageUitls
  18.     {
  19.         /// <summary>
  20.         /// Resizes images to a specific System.Drawing.Size, with the option to preserve
  21.         /// the aspect ratio of the image.
  22.         /// </summary>
  23.         /// <param name="image">System.Drawing.Image to resize</param>
  24.         /// <param name="size">System.Drawing.Size for the new image</param>
  25.         /// <param name="preserveAspectRatio">Whether or not to preserve the aspect ratio</param>
  26.         /// <param name="frameToSize">Whether or not to include a white frame. Can be used when the new size is not the same aspect ratio as the old size.</param>
  27.         /// <returns>System.Drawing.Image object containing the resized image</returns>
  28.         public static Image ResizeImage(Image image, Size size, bool preserveAspectRatio = true, bool frameToSize = false)
  29.         {
  30.             int newWidth;
  31.             int newHeight;
  32.             if (preserveAspectRatio)
  33.             {
  34.                 int originalWidth = image.Width;
  35.                 int originalHeight = image.Height;
  36.                 float percentWidth = (float)size.Width / (float)originalWidth;
  37.                 float percentHeight = (float)size.Height / (float)originalHeight;
  38.                 float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
  39.                 newWidth = (int)(originalWidth * percent);
  40.                 newHeight = (int)(originalHeight * percent);
  41.             }
  42.             else
  43.             {
  44.                 newWidth = size.Width;
  45.                 newHeight = size.Height;
  46.             }
  47.             Image newImage;
  48.             if (frameToSize)
  49.             {
  50.                 newImage = new Bitmap(size.Width, size.Height);
  51.             }
  52.             else
  53.             {
  54.                 newImage = new Bitmap(newWidth, newHeight);
  55.             }
  56.             using (Graphics graphicsHandle = Graphics.FromImage(newImage))
  57.             {
  58.                 graphicsHandle.FillRectangle(Brushes.White, 0, 0, newImage.Width, newImage.Height);
  59.                 graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
  60.                 graphicsHandle.DrawImage(image, (newImage.Width - newWidth) / 2, (newImage.Height - newHeight) / 2, newWidth, newHeight);
  61.             }
  62.             return newImage;
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Takes a System.Drawing.Image object and converts it to a new System.Drawing.Image object.
  67.         /// Useful if the ImageToByteArray() function doesn't work.
  68.         /// </summary>
  69.         /// <param name="imageIn"></param>
  70.         /// <returns>System.Drawing.Image object</returns>
  71.         public static Image ImageToNewImage(Image imageIn)
  72.         {
  73.             Image newImage;
  74.             newImage = new Bitmap(imageIn.Width, imageIn.Height);
  75.             using (Graphics graphicsHandle = Graphics.FromImage(newImage))
  76.             {
  77.                 graphicsHandle.DrawImage(imageIn, 0, 0, imageIn.Width, imageIn.Height);
  78.             }
  79.             return newImage;
  80.         }
  81.  
  82.         /// <summary>
  83.         /// Gets the System.Drawing.Imaging.ImageCodecInfo object for a specified System.Drawing.Imaging.ImageFormat
  84.         /// </summary>
  85.         /// <param name="format"></param>
  86.         /// <returns></returns>
  87.         public static ImageCodecInfo GetEncoder(ImageFormat format)
  88.         {
  89.             ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
  90.             foreach (ImageCodecInfo codec in codecs)
  91.             {
  92.                 if (codec.FormatID == format.Guid)
  93.                 {
  94.                     return codec;
  95.                 }
  96.             }
  97.             return null;
  98.         }
  99.  
  100.         /// <summary>
  101.         /// Takes a System.Drawing.Image byte array and converts it to a byte array of a different format.
  102.         /// Can be used with File.WriteAllBytes(string, byte[]) to do image format conversion.
  103.         /// </summary>
  104.         /// <param name="byteArrayIn"></param>
  105.         /// <param name="imageFormat">System.Drawing.Imaging.ImageFormat to save as</param>
  106.         /// <param name="jpegQuality"></param>
  107.         /// <returns>byte[] containing the converted byte array</returns>
  108.         public static byte[] ImageByteArrayToImageByteArray(byte[] byteArrayIn, ImageFormat imageFormat, Int64 jpegQuality = 80)
  109.         {
  110.             using (Image tempImage = Image.FromStream(new MemoryStream(byteArrayIn)))
  111.             {
  112.                 using (MemoryStream ms = new MemoryStream())
  113.                 {
  114.                     if (imageFormat == ImageFormat.Jpeg)
  115.                     {
  116.                         ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
  117.                         System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
  118.                         EncoderParameter encoderParameter = new EncoderParameter(myEncoder, jpegQuality);
  119.                         EncoderParameters encoderParameters = new EncoderParameters(1);
  120.                         encoderParameters.Param[0] = encoderParameter;
  121.                         tempImage.Save(ms, jpgEncoder, encoderParameters);
  122.                     } else
  123.                     {
  124.                         tempImage.Save(ms, imageFormat);
  125.                     }
  126.                     return ms.ToArray();
  127.                 }
  128.             }
  129.         }
  130.  
  131.         /// <summary>
  132.         /// Converts from a System.Drawing.Image object to a System.Drawing.Image byte array in the specified System.Drawing.Imaging.ImageFormat
  133.         /// </summary>
  134.         /// <param name="imageIn">System.Drawing.Image object to convert</param>
  135.         /// <param name="imageFormat">System.Drawing.Imaging.ImageFormat to save as</param>
  136.         /// <param name="jpegQuality">Quality of the JPEG image, as an integer 0-100. Ignored if imageFormat is not ImageFormat.Jpeg</param>
  137.         /// <returns>byte[] containing the contents of the System.Drawing.Image object</returns>
  138.         public static byte[] ImageToImageByteArray(Image imageIn, ImageFormat imageFormat, Int64 jpegQuality = 80)
  139.         {
  140.  
  141.             using (MemoryStream ms = new MemoryStream())
  142.             {
  143.                 if (imageFormat == ImageFormat.Jpeg)
  144.                 {
  145.                     ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
  146.                     System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
  147.                     EncoderParameter encoderParameter = new EncoderParameter(myEncoder, jpegQuality);
  148.                     EncoderParameters encoderParameters = new EncoderParameters(1);
  149.                     encoderParameters.Param[0] = encoderParameter;
  150.                     imageIn.Save(ms, jpgEncoder, encoderParameters);
  151.                 }
  152.                 else
  153.                 {
  154.                     imageIn.Save(ms, imageFormat);
  155.                 }
  156.                 return ms.ToArray();
  157.             }
  158.         }
  159.  
  160.         /// <summary>
  161.         /// Converts from a System.Drawing.Image byte array to a System.Drawing.Bitmap object
  162.         /// </summary>
  163.         /// <param name="byteArrayIn">Byte array of the System.Drawing.Image object to convert</param>
  164.         /// <returns>System.Drawing.Image object</returns>
  165.         public static Image ImageByteArrayToImage(byte[] byteArrayIn)
  166.         {
  167.             using (MemoryStream ms = new MemoryStream(byteArrayIn))
  168.             {
  169.                 Image returnImage = Bitmap.FromStream(ms);
  170.                 return returnImage;
  171.             }
  172.         }
  173.  
  174.         /// <summary>
  175.         /// Converts from a System.Drawing.Bitmap object to a System.Drawing.Bitmap byte array in the specified System.Drawing.Imaging.ImageFormat
  176.         /// </summary>
  177.         /// <param name="imageIn">System.Drawing.Bitmap object to convert</param>
  178.         /// <param name="imageFormat">System.Drawing.Imaging.ImageFormat to save as</param>
  179.         /// <param name="jpegQuality">Quality of the JPEG image, as an integer 0-100. Ignored if imageFormat is not ImageFormat.Jpeg</param>
  180.         /// <returns>byte[] containing the contents of the System.Drawing.Bitmap object</returns>
  181.         public static byte[] BitmapToBitmapByteArray(Bitmap imageIn, ImageFormat imageFormat, Int64 jpegQuality = 80)
  182.         {
  183.  
  184.             using (MemoryStream ms = new MemoryStream())
  185.             {
  186.                 if (imageFormat == ImageFormat.Jpeg)
  187.                 {
  188.                     ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
  189.                     System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
  190.                     EncoderParameter encoderParameter = new EncoderParameter(myEncoder, jpegQuality);
  191.                     EncoderParameters encoderParameters = new EncoderParameters(1);
  192.                     encoderParameters.Param[0] = encoderParameter;
  193.                     imageIn.Save(ms, jpgEncoder, encoderParameters);
  194.                 }
  195.                 else
  196.                 {
  197.                     imageIn.Save(ms, imageFormat);
  198.                 }
  199.                 return ms.ToArray();
  200.             }
  201.         }
  202.  
  203.         /// <summary>
  204.         /// Converts from a System.Drawing.Bitmap byte array to a System.Drawing.Image object
  205.         /// </summary>
  206.         /// <param name="byteArrayIn">Byte array of the System.Drawing.Bitmap object to convert</param>
  207.         /// <returns>System.Drawing.Image object</returns>
  208.         public static Image BitmapByteArrayToImage(byte[] byteArrayIn)
  209.         {
  210.             using (MemoryStream ms = new MemoryStream(byteArrayIn))
  211.             {
  212.                 Image returnImage = Bitmap.FromStream(ms);
  213.                 return returnImage;
  214.             }
  215.         }
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement