0

How do I reduce the dimension of an image in C#? I am working in .NET 1.1.

Example: Reduce dimension 800x600 to 400x400

Martin B
  • 23,670
  • 6
  • 53
  • 72
user158182
  • 97
  • 1
  • 9
  • Do you want to resize the image (which may potentially distort it), or crop it (remove the portion of it outside the new size)? – Ian Kemp Aug 27 '09 at 07:20

2 Answers2

5

see here

public Image ResizeImage( Image img, int width, int height )
{
    Bitmap b = new Bitmap( width, height ) ;
    using(Graphics g = Graphics.FromImage( (Image ) b ))
    {       
         g.DrawImage( img, 0, 0, width, height ) ;
    }

    return (Image ) b ;
}
Community
  • 1
  • 1
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
0

You need to resize the image.

System.Drawing.Image imgToResize = null;
Bitmap bmpImage = null;
Graphics grphImage = null;

try
{
    imgToResize = System.Drawing.Image.FromFile ( "image path" );

    bmpImage = new Bitmap ( resizeWidth , resizeheight );
    grphImage = Graphics.FromImage ( ( System.Drawing.Image ) bmpImage );

    grphImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
    grphImage.PixelOffsetMode = PixelOffsetMode.HighQuality;
    grphImage.SmoothingMode = SmoothingMode.AntiAlias;
    grphImage.DrawImage ( imgToResize , 0 , 0, resizeWidth , resizeheight );    

    imgToResize.Dispose();  
    grphImage.Dispose();

    bmpImage.Save ( "save location" );
}

catch ( Exception ex )
{
   // your exception handler
}
finally
{               
    bmpImage.Dispose();
}
rahul
  • 184,426
  • 49
  • 232
  • 263