tags:

views:

121

answers:

2

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

Example: Reduce dimension 800x600 to 400x400

A: 

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
+1  A: 

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 ;
}
ArsenMkrt