views:

203

answers:

4

Anyone knows of any good Image resize API for ASP.net? TIA

+3  A: 

Checkout System.Drawing Namespace

MSDN Documentation

Resizing Image - Stack Overflow Question

Elijah Glover
Thank you. This is really helpful.
Murali Bala
+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
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
System.Drawing.Image is an API
Elijah Glover
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
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.

http://www.imagemagick.org/script/index.php

Ronnie
.net library is no longer being developed.There is no point unless you know how to use p/invoke
Elijah Glover
+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
Excellent! I will give that a try.
Murali Bala
Also guys check this site out:http://nathanaeljones.com/products/asp-net-image-resizer/
Murali Bala
I have seen his software before. Its better to push images out when publishing content. To Prevent DDOS Attacks.
Elijah Glover