减少图像的尺寸

时间:2009-08-27 07:13:37

标签: c# image

如何减少C#中图像的尺寸?我在.NET 1.1中工作。

示例:将尺寸800x600缩小为400x400

2 个答案:

答案 0 :(得分:5)

请参阅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 ;
}

答案 1 :(得分:0)

您需要调整图片大小。

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