Anyone knows of any good Image resize API for ASP.net?
Asked
Active
Viewed 3,139 times
6 Answers
2
The ImageResizer library is actively developed, maintained, and supported (since 2007). It is up-to-date with the latest performance techniques, features, and has a simple API. It's safe, secure, reliable, and powers hundreds of commercial web sites.
It's compatible with ASP.NET 2.0 through 4.0, MVC, and is designed to be extremely fast with IIS 7.

Lilith River
- 16,204
- 2
- 44
- 76
1
Here's what I use:
internal static System.Drawing.Image FixedSize(System.Drawing.Image imgPhoto, int Width, int Height)
{
int sourceWidth = Convert.ToInt32(imgPhoto.Width);
int sourceHeight = Convert.ToInt32(imgPhoto.Height);
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Black);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
Usage is pretty simple:
System.Drawing.Image orignalImage = Image.FromFile(filePath);
System.Drawing.Image resizedImage = FixedSize(originalImage, 640, 480);

Jim
- 505
- 3
- 13
-
1Also guys check this site out: http://nathanaeljones.com/products/asp-net-image-resizer/ – Murali Bala Aug 19 '09 at 14:06
-
I have seen his software before. Its better to push images out when publishing content. To Prevent DDOS Attacks. – Elijah Glover Aug 19 '09 at 14:26
-
@Elijah: DOS and DDOS attacks are prevented at several levels - cache size limiting and resolution limiting. Strangely, sites like Facebook and Flickr seem to be quite happy with dynamic image resizing. Also, if you're big enough to make enemies, you should invest in a DOS attack protection layer. With ASP.NET, dynamic image resizing is *not* the weak link for DOS attacks. – Lilith River Jul 17 '11 at 09:06
0
Resizing images is simple enough not to need an API. I wrote my own for this task. here's a bit if code to start you out down that path.
// get original image
System.Drawing.Image orignalImage = Image.FromFile(originalPath);
// create a new image at the desired size
System.Drawing.Bitmap newImage = new Bitmap(450, 338);
// create grpahics object to draw with
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newImage);
//draw the new image
g.DrawImage(orignalImage, r);
// save the new image
newImage.Save(System.IO.Path.Combine(OUTPUT_FOLDER_PATH , ImageName.Replace(" ", "")));

David
- 72,686
- 18
- 132
- 173
-
Thanks David. I have also written similar code for handling image resizing (w/aspect ratio). The problem i am facing is when i want to re-size say - an image of 160px by 110px to 48px by 48px. The result is not good. Not sure if there is a solution but thought a third party vendor would/could have solved this issue. Thanks. – Murali Bala Aug 19 '09 at 13:54
-
-
1To fix your problem you should find out what dimension of your image is larger and scale and crop. System.Drawing Will Provide :D – Elijah Glover Aug 19 '09 at 13:57
-
No, it is not simple. Please read [the image resizing pitfalls](http://nathanaeljones.com/163/20-image-resizing-pitfalls/)! – Lilith River Jul 17 '11 at 09:03
0
I'll provide this as an alternative to the other answers provided. Image Magick is a very powerful and mature image processing library that you can use from .net. I've had a lot of success with it.

Ron
- 1,786
- 19
- 20
-
.net library is no longer being developed. There is no point unless you know how to use p/invoke – Elijah Glover Aug 19 '09 at 13:53