Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir,


How to reduce image size in asp.net with same clarity

I am uploading image using file upload control.

its size Example:
My Image Size is => 1 MB to 3MB.
it will reduce below 100 KB size.


and
it will display same clarity what image original size is.

Please tell me how to make a code for reduce image size???


By
Posted

Use following function to reduce image size. 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
 
Comments
Member 9534579 23-Oct-13 3:52am    
What we use in place of HttpContext?
Member 9534579 23-Oct-13 3:56am    
What parameters we need to pass when we call this method to another method for imgae saving?
Mukund Thakker 23-Oct-13 5:43am    
int iMinAircraftImgWidth = 500;
int iMinAircraftImgHeight = 372;
ResizeImage(imagename, "imagepath", "imagepath", iMinAircraftImgWidth, iMinAircraftImgHeight, strNewFileName);
Member 9534579 23-Oct-13 7:33am    
Thanks
Member 10752740 16-Apr-14 3:59am    
i want only reduce image file size
Hi
Use following code to reduce size without loss of clarity
C#
Image img;
            img.GetThumbnailImage(400,400,null,IntPtr.Zero) 
 
Share this answer
 
Comments
Mohankumar.Engain 9-Apr-12 5:27am    
My image sizes, I declared Only 100px * 100px.
Then how to make it reply me.
Muralikrishna8811 9-Apr-12 5:29am    
then replace 400 with 100
by default it takes as pixels
A very simple way to decrease size of an image or generating a thumbnail

check this out

Creating Thumbnail Image by keeping Aspect Ratio using C#
 
Share this answer
 
v2
 
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