Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created September 7, 2009 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpoehls/182417 to your computer and use it in GitHub Desktop.
Save jpoehls/182417 to your computer and use it in GitHub Desktop.
ImageResizer + ImageUtility
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
namespace ImageTools
{
public class ImageResizer
{
/// <summary>
/// Opens an image file, resizes it and returns the resized copy
/// as a stream. The image is scaled to the given width and
/// returned as a JPEG.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="newWidth">Width of the image to create.</param>
/// <returns>Stream of the resized image file.</returns>
public static byte[] GetResizedImage(string fileName, int newWidth)
{
var file = new FileInfo(fileName);
using (Image image = Image.FromFile(file.FullName))
{
using (var stream = new MemoryStream())
{
if (image.Width > newWidth && GraphicsSupportsPixelFormat(image.PixelFormat))
{
int newHeight = image.Height * newWidth / image.Width;
using (Image thumbnail = new Bitmap(newWidth, newHeight, image.PixelFormat))
{
Graphics thumbGraph = Graphics.FromImage(thumbnail);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var rect = new Rectangle(0, 0, newWidth, newHeight);
thumbGraph.DrawImage(image, rect);
thumbnail.Save(stream, ImageFormat.Jpeg);
}
}
else
{
image.Save(stream, ImageFormat.Jpeg);
}
byte[] content = stream.ToArray();
return content;
}
}
}
private static bool GraphicsSupportsPixelFormat(PixelFormat format)
{
// these pixel formats are not supported by the Graphics.FromImage() method
// http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx
if (format == PixelFormat.Format1bppIndexed ||
format == PixelFormat.Format4bppIndexed ||
format == PixelFormat.Format8bppIndexed ||
format == PixelFormat.Undefined ||
format == PixelFormat.DontCare ||
format == PixelFormat.Format16bppArgb1555 ||
format == PixelFormat.Format16bppGrayScale)
{
return false;
}
return true;
}
}
}
public static class ImageUtility
{
private static void SaveImage(byte[] imageBytes, int resizeToWidth, string saveToFile)
{
var myCallback =
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
using (var imgStream = new MemoryStream(imageBytes))
{
using (var image = System.Drawing.Image.FromStream(imgStream))
{
using (var outStream = new MemoryStream())
{
if (image.Width > resizeToWidth)
{
using (System.Drawing.Image thumbnail = image.GetThumbnailImage(resizeToWidth, image.Height * resizeToWidth / image.Width, myCallback, IntPtr.Zero))
{
thumbnail.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
else
{
image.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
byte[] content = outStream.ToArray();
File.WriteAllBytes(saveToFile, content);
}
}
}
}
private static bool StreamImage(string fileName, int newWidth)
{
var context = HttpContext.Current;
var myCallback =
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
if (File.Exists(fileName))
{
string cacheKey = string.Format("thumbnail:{0}:{1}", fileName, newWidth);
if (context.Cache[cacheKey] == null)
{
using (System.Drawing.Image image = System.Drawing.Image.FromFile(fileName))
{
using (var stream = new MemoryStream())
{
if (image.Width > newWidth)
{
using (System.Drawing.Image thumbnail = image.GetThumbnailImage(newWidth, image.Height * newWidth / image.Width, myCallback, IntPtr.Zero))
{
thumbnail.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
else
{
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
byte[] content = stream.ToArray();
context.Cache.Insert(cacheKey, content, new CacheDependency(fileName));
}
}
}
var cachedImage = (byte[])context.Cache[cacheKey];
context.Response.BinaryWrite(cachedImage);
return true;
}
return false;
}
/// <summary>
/// Required, but not used
/// </summary>
/// <returns>true</returns>
private static bool ThumbnailCallback()
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment