Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
can somebody tells me how to decrease the number of pixels of an image in c#
Posted
Comments
Dinesh Mani 13-Feb-12 5:35am    
Please be more clear. Do you wish to crop the image or reduce the DPI? Do you want to do this when uploading the image or displaying it?

Have a look at the Image.GetThumbnailImage Method[^] - it probably provides what you are looking for. Mind you, with that little info, it is difficult to tell! :laugh:
 
Share this answer
 
Just have written a method for it!
C#
public  static Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }

example:
C#
Image myNewImage = resizeImage(myImage, New Size(1000, 1000);
You now have an image this has maximum of 1000 pixles on the longest side of the original image.

To safe your image use
C#
myNewImage.Save(NewFileName);
 
Share this answer
 
Comments
saifullahiit 13-Feb-12 5:12am    
it gives an error InterpolationMode does not exist in current context
Herman<T>.Instance 13-Feb-12 5:24am    
do you use: using System.Drawing.Drawing2D; ?

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