I\'ve got an webapplication where users can upload images. The current problem i\'m running into is that the images being uploaded are being saved to the database in the ori
We use the latter approach - I can't comment on performance but it certainly makes handling dependencies simpler.
However, one thing to note is that the above code is probably too simple if your users are able to upload images in all sorts of formats. The underlying library (GDI+) has issues with a lot of color formats, but it also is dependent on the OS version. Here's the core of the code we use:
// GDI+ has problems with lots of image formats, and it also chokes on unknown ones (like CMYK).
// Therefore, we're going to take a whitelist approach.
// see http://bmpinroad.blogspot.com/2006/04/file-formats-pixel-formats.html
// also see http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c626a478-e5ef-4a5e-9a73-599b3b7a6ecc
PixelFormat format = originalImage.PixelFormat;
if (format == PixelFormat.Format16bppArgb1555 ||
format == PixelFormat.Format64bppArgb)
{
// try to preserve transparency
format = PixelFormat.Format32bppArgb;
}
else if (format == PixelFormat.Format64bppPArgb)
{
// try to preserve pre-multiplied transparency
format = PixelFormat.Format32bppPArgb;
}
else if (format != PixelFormat.Format24bppRgb && format != PixelFormat.Format32bppRgb)
{
format = PixelFormat.Format24bppRgb;
}
// GIF saving is probably still an issue. If we ever need to tackle it, see the following:
// http://support.microsoft.com/kb/319061
// http://www.bobpowell.net/giftransparency.htm
// http://support.microsoft.com/kb/318343
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, format))
{
using (Graphics Canvas = Graphics.FromImage(newImage))
{
using (ImageAttributes attr = new ImageAttributes())
{
attr.SetWrapMode(WrapMode.TileFlipXY);
Canvas.SmoothingMode = SmoothingMode.AntiAlias;
Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
Canvas.DrawImage(originalImage, new Rectangle(new Point(0, 0), newSize), srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, GraphicsUnit.Pixel, attr);
newImage.Save(outputImageStream, originalImage.RawFormat);
}
}
}
I've never used ImageMagic, but I have used the GDI+ image resize functions, including on a site that generates and resizes 100,000+ images a day without performance issues.
I would say using the GDI+ methods are just fine. Don't worry about wrapping an external tool or framework.
I've used ImageGen in Umbraco sites. (It's certainly not tied to Umbraco, it's good for any ASP.NET app, it just so happened that some of the Umbraco packages I was using required it.) It's simple to use, and you might be able to get away with the free version...