Anyone knows of any good Image resize API for ASP.net? TIA
Thank you. This is really helpful.
Murali Bala
2009-08-19 13:59:54
+1
A:
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 Stratton
2009-08-19 13:45:32
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
2009-08-19 13:54:05
To 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
2009-08-19 13:57:03
A:
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.
Ronnie
2009-08-19 13:50:40
.net library is no longer being developed.There is no point unless you know how to use p/invoke
Elijah Glover
2009-08-19 13:53:33
+1
A:
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
2009-08-19 14:03:59
Also guys check this site out:http://nathanaeljones.com/products/asp-net-image-resizer/
Murali Bala
2009-08-19 14:06:21
I have seen his software before. Its better to push images out when publishing content. To Prevent DDOS Attacks.
Elijah Glover
2009-08-19 14:26:49