A file I havent touched in 2 yrs. It looks crappy at 1st sight but it does resize jpegs.
[VB]
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Net;
namespace HB.Utility
{
/// <summary>
/// Represents a JPEG image.
/// </summary>
[Serializable]
public class Jpeg : IDisposable, ICloneable
{
/// <summary>
/// Loads a JPEG file as an <see cref="Image"/> from a website or a local file.
/// </summary>
/// <param name="source">The path of the file.</param>
/// <exception cref="ArgumentNullException">When parameter source is null.</exception>
public Jpeg(string source)
{
if(source == null)
throw new ArgumentNullException("source");
if(source.StartsWith("http://"))
image = LoadFromWeb(source);
else
image = Image.FromFile(source, true);
compression = 50;
quality = 100;
disposed = false;
}
/// <summary>
/// Copy constructor.
/// </summary>
/// <param name="jpeg">The <see cref="Jpeg"/> to copy.</param>
/// <exception cref="ArgumentNullException">When paremeter jpeg is null.</exception>
/// <remarks>Creates a deep copy of a <see cref="Jpeg"/> object.</remarks>
public Jpeg(Jpeg jpeg)
{
if(jpeg == null)
throw new ArgumentNullException("jpeg");
compression = jpeg.Compression;
disposed = jpeg.disposed;
image = (Image) image.Clone();
quality = jpeg.Quality;
}
/// <summary>
/// Gets or sets the compression value of the JPEG.
/// </summary>
public long Compression
{
get
{
IsDisposed();
return compression;
}
set
{
IsDisposed();
if(value < 0)
throw new InvalidOperationException("Compression cannot be less than zero.");
compression = value;
}
}
/// <summary>
/// Gets or sets the quality value of the JPEG.
/// </summary>
public long Quality
{
get
{
IsDisposed();
return quality;
}
set
{
IsDisposed();
if(value < 0)
throw new InvalidOperationException("Quality cannot be less than zero.");
quality = value;
}
}
/// <summary>
/// Gets the <see cref="ImageCodecInfo"/> for JPEG file types.
/// </summary>
static Jpeg()
{
imageCodecInfo = null;
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
foreach(ImageCodecInfo encoder in encoders)
if(encoder.MimeType == "image/jpeg")
imageCodecInfo = encoder;
if(imageCodecInfo == null)
throw new Exception("JPEG file format is not supported on this machine.");
}
/// <summary>
/// Resizes an image proportionally by height.
/// </summary>
/// <param name="height">The new height of the image.</param>
/// <returns>The resized image.</returns>
/// <exception cref="ArgumentOutOfRangeException">When parameter height is less than zero.</exception>
public void ResizeByHeight(int height)
{
IsDisposed();
if(height < 0)
throw new ArgumentOutOfRangeException("height", height, "Height cannot be less than zero.");
float rate = (float) height/image.Height;
height = (int) (image.Height * rate);
int width = (int) (image.Width * rate);
Resize(width, height);
}
/// <summary>
/// Resizes the <see cref="Image"/> of the current instance.
/// </summary>
/// <param name="width">The new width of the image.</param>
/// <param name="height">The new height of the image.</param>
/// <exception cref="ApplicationException">When an error occurred while trying to resize the image.</exception>
private void Resize(int width, int height)
{
Image target = (Image) new Bitmap(width, height);
using(System.Drawing.Graphics gThumb = System.Drawing.Graphics.FromImage(target))
{
try
{
gThumb.DrawImage(image, 0, 0, width, height);
image.Dispose();
image = target;
}
catch(Exception e)
{
try
{
target.Dispose();
}
catch
{
}
string msg = "An error occurred while trying to resize the image.";
throw new ApplicationException(msg, e);
}
}
}
/// <summary>
/// Resizes an image proportionally by width.
/// </summary>
/// <param name="width">The new width of the image.</param>
/// <returns>The resized image.</returns>
/// <exception cref="ArgumentOutOfRangeException">When parameter width is less than zero.</exception>
public void ResizeByWidth(int width)
{
IsDisposed();
if(width < 0)
throw new ArgumentOutOfRangeException("width", width, "Width cannot be less than zero.");
float rate = (float) width/image.Width;
width = (int) (image.Width * rate);
int height = (int) (image.Height * rate);
Resize(width, height);
}
/// <summary>
/// Loads an image from a URL.
/// </summary>
/// <param name="url">The URL of the image.</param>
/// <returns>The downloaded imahe.</returns>
/// <remarks>No exception handling is performed.</remarks>
private static Image LoadFromWeb(string url)
{
HttpWebResponse res = null;
try
{
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
res = (HttpWebResponse) req.GetResponse();
return Image.FromStream(res.GetResponseStream(), true);
}
finally
{
try
{
res.Close();
}
catch{}
}
}
/// <summary>
/// Saves the <see cref="Jpeg"/>.
/// </summary>
/// <param name="filename">The file path to save the <see cref="Jpeg"/> as.</param>
public void Save(string filename)
{
IsDisposed();
Save(filename, image);
}
/// <summary>
/// Calls <see cref="Dispose"/> if <see cref="Dispose"/> has not already been called.
/// </summary>
~Jpeg()
{
try
{
if(!disposed)
Dispose();
}
catch{}
}
/// <summary>
/// Disposes the underlying <see cref="Image"/>.
/// </summary>
public void Dispose()
{
IsDisposed();
image.Dispose();
GC.SuppressFinalize(this);
disposed = true;
}
/// <summary>
/// Determines if this instance of <see cref="Jpeg"/> has been disposed.
/// </summary>
/// <exception cref="ObjectDisposedException">When this instance of <see cref="Jpeg"/>
/// has been disposed.</exception>
private void IsDisposed()
{
if(disposed)
throw new ObjectDisposedException(null);
}
/// <summary>
/// Saves an <see cref="Image"/> as a JPEG.
/// </summary>
/// <param name="filename">the file path to save the <see cref="Jpeg"/> as.</param>
/// <param name="image">The <see cref="Image"/> to save.</param>
private void Save(string filename, Image image)
{
EncoderParameters parameters = new EncoderParameters(2);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
parameters.Param[1] = new EncoderParameter(Encoder.Compression, compression);
image.Save(filename, JpegCodecInfo, parameters);
}
/// <summary>
/// Gets the currently loaded image.
/// </summary>
public Image LoadedImage
{
get
{
IsDisposed();
return image;
}
}
/// <summary>
/// Gets the JPEG image codec info.
/// </summary>
private static ImageCodecInfo JpegCodecInfo
{
get
{
return imageCodecInfo;
}
}
/// <summary>
/// Gets a deep copy of the invoked <see cref="Jpeg"/>.
/// </summary>
/// <returns>A deep copy of the invoked <see cref="Jpeg"/>.</returns>
/// <remarks>A clone is created using the copy constructor.</remarks>
public object Clone()
{
IsDisposed();
return new Jpeg(this);
}
/// <summary>
/// Stores whether the object has been disposed or not.
/// </summary>
private bool disposed;
/// <summary>
/// Stores the JPEG <see cref="Image"/>.
/// </summary>
private Image image;
/// <summary>
/// Stores the quality value of the <see cref="Jpeg"/>.
/// </summary>
private long quality;
/// <summary>
/// Stores the compression value of the <see cref="Jpeg"/>.
/// </summary>
private long compression;
private static ImageCodecInfo imageCodecInfo;
}
}
[/VB]