Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET WCF
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 8-Apr-12 22:42pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Use following function to reduce image size. It will regenerate image with defined height and width.
 
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();
    }
  Permalink  
Comments
Member 9534579 at 23-Oct-13 3:52am
   
What we use in place of HttpContext?
Member 9534579 at 23-Oct-13 3:56am
   
What parameters we need to pass when we call this method to another method for imgae saving?
Mukund Thakker at 23-Oct-13 5:43am
   
int iMinAircraftImgWidth = 500;
int iMinAircraftImgHeight = 372;
ResizeImage(imagename, "imagepath", "imagepath", iMinAircraftImgWidth, iMinAircraftImgHeight, strNewFileName);
Member 9534579 at 23-Oct-13 7:33am
   
Thanks
Member 10752740 at 16-Apr-14 3:59am
   
i want only reduce image file size
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi
 
Use following code to reduce size without loss of clarity
 
Image img;
            img.GetThumbnailImage(400,400,null,IntPtr.Zero) 
  Permalink  
Comments
mohankumar345 at 9-Apr-12 5:27am
   
My image sizes, I declared Only 100px * 100px.
Then how to make it reply me.
Muralikrishna8811 at 9-Apr-12 5:29am
   
then replace 400 with 100
by default it takes as pixels
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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#
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 10 Sep 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100