Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Created June 4, 2012 13:29
Show Gist options
  • Save Dynyx/2868383 to your computer and use it in GitHub Desktop.
Save Dynyx/2868383 to your computer and use it in GitHub Desktop.
Thumbnail generator
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Net;
namespace Thumbnailer
{
public static class Thumbnail
{
/// <summary>
///
/// </summary>
/// <param name="source">The image to crop</param>
/// <param name="dimension">The maximum size of the resulting image</param>
/// <returns></returns>
private static Image Crop(Image source, int dimension)
{
Image resizedSource = Resize(source, dimension);
int diffX = (resizedSource.Width - dimension);
int diffY = (resizedSource.Height - dimension);
int x = diffX > 1 ? diffX / 2 : 0;
int y = diffY > 1 ? diffY / 2 : 0;
var cropRectangle = new Rectangle(x, y, dimension, dimension);
var sourceImage = new Bitmap(resizedSource);
Bitmap croppedImage = sourceImage.Clone(cropRectangle, sourceImage.PixelFormat);
return croppedImage;
}
/// <summary>
///
/// </summary>
/// <param name="path">The full path of the image to download</param>
/// <param name="dimension">The maximum size of the resulting image</param>
/// <param name="maintainAspect">Determines if the resulting image should maintain its original aspect ratio</param>
/// <returns></returns>
public static Image Generate(string path, int dimension, bool maintainAspect)
{
if (File.Exists(path))
{
Image image = Image.FromFile(path);
return maintainAspect ? Resize(image, dimension) : Crop(image, dimension);
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="url">The URL of the image to download</param>
/// <param name="dimension">The maximum size of the resulting image</param>
/// <param name="maintainAspect">Determines if the resulting image should maintain its original aspect ratio</param>
/// <returns></returns>
public static Image Generate(Uri url, int dimension, bool maintainAspect)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.AllowWriteStreamBuffering = true;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
request.Referer = "http://www.google.com/";
request.Timeout = 30000;
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
if (stream != null)
{
Image image = Image.FromStream(stream);
response.Close();
return maintainAspect ? Resize(image, dimension) : Crop(image, dimension);
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="source">The image to resize</param>
/// <param name="dimension">The maximum size of the resulting image</param>
/// <returns></returns>
private static Image Resize(Image source, int dimension)
{
float factor;
if (source.Height <= source.Width)
factor = (dimension + 1) / (float)source.Height;
else
factor = (dimension + 1) / (float)source.Width;
var destWidth = (int)(source.Width * factor);
var destHeight = (int)(source.Height * factor);
var bmp = new Bitmap(destWidth, destHeight);
Graphics gfx = Graphics.FromImage(bmp);
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.DrawImage(source, 0, 0, destWidth, destHeight);
gfx.Dispose();
return bmp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment