2009-08-27 167 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 ; 
}