Advertisement
Willcode4cash

Image/base64 Extensions

Apr 12th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. public static class ImageExtensions
  2. {
  3.     /*
  4.      *  This is free and unencumbered software released into the public domain.
  5.      * 
  6.      *  Anyone is free to copy, modify, publish, use, compile, sell, or
  7.      *  distribute this software, either in source code form or as a compiled
  8.      *  binary, for any purpose, commercial or non-commercial, and by any
  9.      *  means.
  10.      * 
  11.      *  In jurisdictions that recognize copyright laws, the author or authors
  12.      *  of this software dedicate any and all copyright interest in the
  13.      *  software to the public domain. We make this dedication for the benefit
  14.      *  of the public at large and to the detriment of our heirs and
  15.      *  successors. We intend this dedication to be an overt act of
  16.      *  relinquishment in perpetuity of all present and future rights to this
  17.      *  software under copyright law.
  18.      * 
  19.      *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20.      *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21.      *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22.      *  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23.      *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24.      *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25.      *  OTHER DEALINGS IN THE SOFTWARE.
  26.      * 
  27.      *  For more information, please refer to <http://unlicense.org>
  28.      */
  29.      
  30.     public static ImageFormat GetImageFormat(this Image image)
  31.     {
  32.         if (image.RawFormat.Equals(ImageFormat.Bmp)) return ImageFormat.Bmp;
  33.         else if (image.RawFormat.Equals(ImageFormat.Emf)) return ImageFormat.Emf;
  34.         else if (image.RawFormat.Equals(ImageFormat.Exif)) return ImageFormat.Exif;
  35.         else if (image.RawFormat.Equals(ImageFormat.Gif)) return ImageFormat.Gif;
  36.         else if (image.RawFormat.Equals(ImageFormat.Icon)) return ImageFormat.Icon;
  37.         else if (image.RawFormat.Equals(ImageFormat.Jpeg)) return ImageFormat.Jpeg;
  38.         else if (image.RawFormat.Equals(ImageFormat.Png)) return ImageFormat.Png;
  39.         else if (image.RawFormat.Equals(ImageFormat.Tiff)) return ImageFormat.Tiff;
  40.         else if (image.RawFormat.Equals(ImageFormat.Wmf)) return ImageFormat.Wmf;
  41.         throw new NotSupportedException("Invalid/Unsupported ImageFormat");
  42.     }
  43.  
  44.     public static string ToBase64(this Image image)
  45.     {
  46.         var imageFormat = image.GetImageFormat();
  47.         return image.ToBase64(imageFormat);
  48.     }
  49.  
  50.     public static string ToBase64(this Image image, ImageFormat format)
  51.     {
  52.         var memoryStream = new MemoryStream();
  53.         image.Save(memoryStream, format);
  54.         var imageBytes = memoryStream.ToArray();
  55.         string base64String = Convert.ToBase64String(imageBytes);
  56.         return base64String;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement