2009-10-16 6 views

Répondre

1

Essayez la méthode suivante:

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

Et la taille du fichier? Je veux réduire la taille du fichier aussi. – VansFannel

0

Avez-vous essayé?

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

Remarque important (http://msdn.microsoft.com/en-us/library/system.drawing.aspx)> Les classes au sein de l'espace de noms System.Drawing ne sont pas pris en charge pour une utilisation dans un service Windows ou ASP.NET. –

0

l'extrait j'utilise toujours:

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

cible de retour;

+1

Remarque importante (http://msdn.microsoft.com/fr-fr/library/system.drawing.aspx)> Les classes dans l'espace de noms System.Drawing ne sont pas prises en charge pour une utilisation dans un service Windows ou ASP.NET. –

Questions connexes