Click here to Skip to main content
15,912,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to create an asp.net webservice that will accept File and it has to create thumbnail of the image file supplied of 200 X 200 pix. please help me with the code to do so.
Posted
Updated 8-Apr-12 5:09am
v2

1 solution

Use following function to generate thumbnail of the page. It will regenerate image with defined height and width.

C#
public static void ResizeImage(string image, string Okey, string key, int width, int height, string newimagename)
    {
        System.Drawing.Image oImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image));
 
        System.Drawing.Image oThumbNail = new System.Drawing.Bitmap(width, height);//, System.Drawing.Imaging.PixelFormat.Format24bppRgb

        System.Drawing.Graphics oGraphic = System.Drawing.Graphics.FromImage(oThumbNail);
 
        oGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
 
        //set smoothing mode to high quality
        oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //set the interpolation mode
        oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        //set the offset mode
        oGraphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
 
        System.Drawing.Rectangle oRectangle = new System.Drawing.Rectangle(0, 0, width, height);
 
        oGraphic.DrawImage(oImg, oRectangle);
 
        if (newimagename == "")
        {
            if (image.Substring(image.LastIndexOf(".")) != ".png")
                oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image), System.Drawing.Imaging.ImageFormat.Jpeg);
            else
                oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image), System.Drawing.Imaging.ImageFormat.Png);
        }
        else
        {
            if (newimagename.Substring(newimagename.LastIndexOf(".")) != ".png")
                oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + newimagename), System.Drawing.Imaging.ImageFormat.Jpeg);
            else
                oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + newimagename), System.Drawing.Imaging.ImageFormat.Png);
        }
        oImg.Dispose();
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900