user158182
user158182

Reputation: 97

Reduce the dimension of an image

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

Example: Reduce dimension 800x600 to 400x400

Upvotes: 0

Views: 1700

Answers (2)

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

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

Upvotes: 5

rahul
rahul

Reputation: 187020

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();
}

Upvotes: 0

Related Questions