2009-10-16 124 views

回答

1

試試下面的方法:System.Drawing命名空間內

public string ResizeImageAndSave(int Width, int Height, string imageUrl, string destPath) 
    { 
     System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imageUrl); 
     double widthRatio = (double)fullSizeImg.Width/(double)Width; 
     double heightRatio = (double)fullSizeImg.Height/(double)Height; 
     double ratio = Math.Max(widthRatio, heightRatio); 
     int newWidth = (int)(fullSizeImg.Width/ratio); 
     int newHeight = (int)(fullSizeImg.Height/ratio); 
     //System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); 
     System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); 
     //DateTime MyDate = DateTime.Now; 
     //String MyString = MyDate.ToString("ddMMyyhhmmss") + imageUrl.Substring(imageUrl.LastIndexOf(".")); 
     thumbNailImg.Save(destPath, ImageFormat.Jpeg); 
     thumbNailImg.Dispose(); 
     return ""; 
    } 
    public bool ThumbnailCallback() { return false; } 
+0

和文件大小?我也想減小文件大小。 – VansFannel 2009-10-17 06:09:33

0

你試過嗎?

public Image resize(Image img, int width, int height) 
    { 
     Bitmap b = new Bitmap(width, height) ; 
     Graphics g = Graphics.FromImage((Image) b) ; 
g.DrawImage(img, 0, 0, width, height) ; 
    g.Dispose() ; 

    return (Image) b ; 
} 
+1

注重要(http://msdn.microsoft.com/en-us/library/system.drawing.aspx)>類不支持在Windows或ASP.NET服務中使用。 – 2009-10-16 11:26:04

0

片斷我總是用:

var target = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb); 
target.SetResolution(source.HorizontalResolution, 
source.VerticalResolution); 

using (var graphics = Graphics.FromImage(target)) 
{ 
    graphics.Clear(Color.White); 
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 

    graphics.DrawImage(source, 
     new Rectangle(destX, destY, destWidth, destHeight), 
     new Rectangle(sourceX, sourceY, source.Width, source.Height), 
     GraphicsUnit.Pixel); 
} 

回報目標;

+1

注意重要(http://msdn.microsoft.com/zh-cn/library/system.drawing.aspx)> System.Drawing命名空間中的類不支持在Windows或ASP.NET服務中使用。 – 2009-10-16 11:26:51

相關問題