Resize Image in C#

I have code that currently works, however there is one small problem, the quality of the image.  I'm not after something PhotoShop quality, but the current code is more for thumbnails rather than 800x600 images.

Basically, it's one of those pages that displays a large image that scales down the image to fit in the browser (and to save loading time), then they click the image for the full picture.

System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(_fileName_));
Size imageSize = new Size(_width_,_height_);
if(resizeImage)
      imageSize = ReSize(image.Width,image.Height);

-------PROBLEM--------
image = image.GetThumbnailImage(imageSize.Width,imageSize.Height,null,IntPtr.Zero);
-------PROBLEM--------

Response.ContentType = "image/jpeg";
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);




ReSize is just my way of scaling the image but keeping the aspect ratio the same.  So i am now looking for what i can use other than GetThumbnailImage (works great for the thumbnails).  I am looking for standard code rather than a third party plugin.

Thanks.
LVL 12
UnexplainedWaysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

InteractiveMindCommented:
Try this:

public Image resizeImage( Image img, int width, int height )
{
    Bitmap b = new Bitmap( width, height ) ;
    Graphics g = Graphics.FromImage( (Image ) b ) ;
   
    g.DrawImage( img, 0, 0, width, height ) ;
    g.Dispose() ;
   
    return (Image ) b ;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
UnexplainedWaysAuthor Commented:
Tested it and works like a charm.  Thanks.
InteractiveMindCommented:
Thanks :-)

In order to change the quality of the resize, the Graphics class contains an enum called InterpolationMode, which can be used to specify the algorithm used.

The available algorithms (built-in to .NET) are:

Bicubic
Bilinear
Default
High
HighQualityBicubic
HighQualityBilinear
Invalid
Low
NearestNeighbor

These are shown here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingdrawing2dinterpolationmodeclasstopic.asp


An example of how to change the quality of the resize, within the above method, would be changing it to:

public Image resizeImage( Image img, int width, int height )
{
    Bitmap b = new Bitmap( width, height ) ;
    Graphics g = Graphics.FromImage( (Image ) b ) ;
   
    g.InterpolationMode = InterpolationMode.Bicubic ;    // Specify here
    g.DrawImage( img, 0, 0, width, height ) ;
    g.Dispose() ;
   
    return (Image ) b ;
}


Good luck.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.