In a rather simple ASP.NET application where the user can upload a image, mostly from his digital camera. I have to resize it to a workable size for the web and a thumbnail. What is here the best practice for? Is there a library that I in a simple way can implement without installing something on the web server.
-
Do you miss some features, which are not implemented by the classes in the System.Drawing namespace?Oliver Hanappi– Oliver Hanappi2009-07-06 07:54:06 +00:00Commented Jul 6, 2009 at 7:54
-
System.Drawing is horribly buggy. Read this list of 28 image resizing pitfalls before you dive in. I wrote the imageresizing.net library to do exactly what you're looking for. It's simple, and has a 1-line API.Lilith River– Lilith River2011-05-28 15:36:59 +00:00Commented May 28, 2011 at 15:36
5 Answers
This thread on SO will probably help you decide:
Comments
Disclaimer: I am the author of this library.
However, it was designed exactly for this purpose, and is very mature and well-tested.
http://nathanaeljones.com/products/asp-net-image-resizer/
I think you will find the included sample application does exactly what you need.
Comments
Look at the BitMap class- you can do this quite easily by specifying the size in the constructor.
So imagine you wanted to half it:
Bitmap firstBitMap; // created somewhere else
// Create a new bitmap from the first, scaling down as we go
Bitmap halfSize = new Bitmap(firstBitMap, new Size(firstBitMap.Width/2, firstBitMap.Height/2));
If you want a higher quality solution, you need to consider the InterpolationMode enumeration.
For the simple scenario you describe you certainly don't need to bother with 3rd party libraries.
Comments
I am no image expert, but I implemented image resizing on a website and used something like this:
public static void ResizeImageHighQuality(string imgFilename, string imgResizedFilename, int resizeH, int resizeW)
{
Image img = Image.FromFile(imgFilename);
int h = 0, w = 0;
if (img.Width > img.Height)
{
w = Convert.ToInt32(resizeW);
h = Convert.ToInt32(w * Convert.ToDouble(img.Height) / Convert.ToDouble(img.Width));
}
else if (img.Height > img.Width)
{
h = Convert.ToInt32(resizeH);
w = Convert.ToInt32(h * Convert.ToDouble(img.Width) / Convert.ToDouble(img.Height));
}
else
{
h = resizeH;
w = resizeW;
}
Image thumbnail = new Bitmap(w, h);
Graphics graphic = Graphics.FromImage(thumbnail);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(img, 0, 0, w, h);
ImageCodecInfo[] Info = ImageCodecInfo.GetImageEncoders();
EncoderParameters Params = new EncoderParameters(1);
Params.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
File.Delete(imgResizedFilename);
FileStream fs = new FileStream(imgResizedFilename, FileMode.CreateNew);
thumbnail.Save(fs, Info[1], Params);
fs.Close();
}
Comments
Using Bitmap (and any other System.Drawing classes) is specifically prohibited in ASP.NET (see the warning near the top of the documentation page).
Using them may result in exceptions such as these:
TypeInitializationException: The type initializer for 'System.Windows.Media.Brush' threw an exception.
at System.Windows.Media.SolidColorBrush..ctor(Color color)
...
'System.Windows.Media.Transform' threw an exception.
at System.Windows.Media.Brush..cctor()Win32Exception: The operation completed successfully
at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
at MS.Win32.MessageOnlyHwndWrapper..ctor()
at System.Windows.Threading.Dispatcher..ctor()
at System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
at System.Windows.Freezable..ctor()
at System.Windows.Media.MatrixTransform..ctor(Matrix matrix)
at System.Windows.Media.Transform..cctor()
and
Win32Exception: The operation completed successfully
at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
at System.Windows.Media.MediaContextNotificationWindow..ctor(MediaContext ownerMediaContext)
at System.Windows.Media.MediaContext..ctor(Dispatcher dispatcher)
Depending on what you are trying to do, Windows Imaging Component may fill your need. We have also had luck by creating a single STA thread and invoking all drawing operations over to that.