Link to home
Start Free TrialLog in
Avatar of UnexplainedWays
UnexplainedWays

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of UnexplainedWays
UnexplainedWays

ASKER

Tested it and works like a charm.  Thanks.
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.