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.FromF ile(Server .MapPath(_ fileName_) );
Size imageSize = new Size(_width_,_height_);
if(resizeImage)
imageSize = ReSize(image.Width,image.H eight);
-------PROBLEM--------
image = image.GetThumbnailImage(im ageSize.Wi dth,imageS ize.Height ,null,IntP tr.Zero);
-------PROBLEM--------
Response.ContentType = "image/jpeg";
image.Save(Response.Output Stream, System.Drawing.Imaging.Ima geFormat.J peg);
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.
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.FromF
Size imageSize = new Size(_width_,_height_);
if(resizeImage)
imageSize = ReSize(image.Width,image.H
-------PROBLEM--------
image = image.GetThumbnailImage(im
-------PROBLEM--------
Response.ContentType = "image/jpeg";
image.Save(Response.Output
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER