Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

I want to squeeze the size of image i.e to reduce the height and width of image...

I have file name(image name) and I had extracted that name by using opendialog and then used the following statement openFileDialog1.FileName;

I have have tried the folowing coding but I'm not able to squeeze the image

MIDL
string file = openFileDialog1.FileName;
               FileInfo f = new FileInfo(file);
               Image img = Image.FromFile(file);
               Bitmap b = new Bitmap(20,20);
               Graphics g = Graphics.FromImage((Image)b);
               g.InterpolationMode = InterpolationMode.HighQualityBicubic;
               g.DrawImage(img, 0, 0, 20,20);
               img.Save(file);


please help me to solve this problem.
thanks in advance
Posted
Updated 9-May-11 10:02am
v2
Comments
Dalek Dave 9-May-11 16:02pm    
Edited for Grammar and Readability.
Sergey Alexandrovich Kryukov 9-May-11 22:50pm    
This is called "resampling".
--SA

1 solution

Create a new Bitmap object of the required size.

Graphics.FromImage will create a graphic object for the new bitmap, then DrawImage to put the original image onto the new bitmap.

Save the new bitmap!

Jobs a good'un.
 
Share this answer
 
v2
Comments
preet88 9-May-11 16:23pm    
hi dalek it was not working i have done this as following
Image image = Image.FromFile(temp); //here temp stores image path from root
Bitmap bmp = new Bitmap(50, 50, PixelFormat.Format24bppRgb);
bmp.SetResolution(50, 50);
Graphics gfx = Graphics.FromImage(bmp);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
gfx.DrawImage(image, new Rectangle(0, 0, 80, 80), 0, 0, 80, 80, GraphicsUnit.Pixel);
// Dispose to free up resources
image.Dispose();
bmp.Dispose();
gfx.Dispose();

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