MrMistreater

Create image thumbnail

May 8th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.IO;
  5. using System.Net;
  6.  
  7. namespace Thumbnailer
  8. {
  9.     public static class Thumbnail
  10.     {
  11.         /// <summary>
  12.         ///
  13.         /// </summary>
  14.         /// <param name="source">The image to crop</param>
  15.         /// <param name="dimension">The maximum size of the resulting image</param>
  16.         /// <returns></returns>
  17.         private static Image Crop(Image source, int dimension)
  18.         {
  19.             Image resizedSource = Resize(source, dimension);
  20.  
  21.             int diffX = (resizedSource.Width - dimension);
  22.             int diffY = (resizedSource.Height - dimension);
  23.  
  24.             int x = diffX > 1 ? diffX / 2 : 0;
  25.             int y = diffY > 1 ? diffY / 2 : 0;
  26.  
  27.             var cropRectangle = new Rectangle(x, y, dimension, dimension);
  28.             var sourceImage = new Bitmap(resizedSource);
  29.             Bitmap croppedImage = sourceImage.Clone(cropRectangle, sourceImage.PixelFormat);
  30.  
  31.             return croppedImage;
  32.         }
  33.  
  34.         /// <summary>
  35.         ///
  36.         /// </summary>
  37.         /// <param name="path">The full path of the image to download</param>
  38.         /// <param name="dimension">The maximum size of the resulting image</param>
  39.         /// <param name="maintainAspect">Determines if the resulting image should maintain its original aspect ratio</param>
  40.         /// <returns></returns>
  41.         public static Image Generate(string path, int dimension, bool maintainAspect)
  42.         {
  43.             if (File.Exists(path))
  44.             {
  45.                 Image image = Image.FromFile(path);
  46.                 return maintainAspect ? Resize(image, dimension) : Crop(image, dimension);
  47.             }
  48.  
  49.             return null;
  50.         }
  51.  
  52.         /// <summary>
  53.         ///
  54.         /// </summary>
  55.         /// <param name="url">The URL of the image to download</param>
  56.         /// <param name="dimension">The maximum size of the resulting image</param>
  57.         /// <param name="maintainAspect">Determines if the resulting image should maintain its original aspect ratio</param>
  58.         /// <returns></returns>
  59.         public static Image Generate(Uri url, int dimension, bool maintainAspect)
  60.         {
  61.             var request = (HttpWebRequest)WebRequest.Create(url);
  62.             request.AllowWriteStreamBuffering = true;
  63.             request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
  64.             request.Referer = "http://www.google.com/";
  65.             request.Timeout = 30000;
  66.  
  67.             WebResponse response = request.GetResponse();
  68.             Stream stream = response.GetResponseStream();
  69.  
  70.             if (stream != null)
  71.             {
  72.                 Image image = Image.FromStream(stream);
  73.                 response.Close();
  74.  
  75.                 return maintainAspect ? Resize(image, dimension) : Crop(image, dimension);
  76.             }
  77.  
  78.             return null;
  79.         }
  80.  
  81.         /// <summary>
  82.         ///
  83.         /// </summary>
  84.         /// <param name="source">The image to resize</param>
  85.         /// <param name="dimension">The maximum size of the resulting image</param>
  86.         /// <returns></returns>
  87.         private static Image Resize(Image source, int dimension)
  88.         {
  89.             float factor;
  90.  
  91.             if (source.Height <= source.Width)
  92.                 factor = (dimension + 1) / (float)source.Height;
  93.             else
  94.                 factor = (dimension + 1) / (float)source.Width;
  95.  
  96.             var destWidth = (int)(source.Width * factor);
  97.             var destHeight = (int)(source.Height * factor);
  98.  
  99.             var bmp = new Bitmap(destWidth, destHeight);
  100.             Graphics gfx = Graphics.FromImage(bmp);
  101.  
  102.             gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
  103.             gfx.DrawImage(source, 0, 0, destWidth, destHeight);
  104.             gfx.Dispose();
  105.  
  106.             return bmp;
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment