2009-08-27 153 views
0

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

例如:800×600 400×400到

+0

你想调整图片的大小(这可能潜在地歪曲),或裁剪它(删除它的部分新的大小之外)? – 2009-08-27 07:20:16

回答

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