15,038,642 members
See more:
can somebody tells me how to decrease the number of pixels of an image in c#
Posted
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?

## Solution 2

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:

## Solution 1

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);`
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)

Top Experts
Last 24hrsThis month
 OriginalGriff 78 Richard MacCutchan 60 Stefan_Lang 40 Dave Kreskowiak 28 CPallini 20
 OriginalGriff 3,235 Richard Deeming 1,848 Richard MacCutchan 1,730 CPallini 1,103 Dave Kreskowiak 811

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