Comment redimensionner une image sur un serveur que je viens de télécharger? J'utilise C# avec .NET Framework 3.5 SP1.ASP.NET: redimensionner (hauteur et largeur) une image téléchargée sur le serveur
Merci!
Comment redimensionner une image sur un serveur que je viens de télécharger? J'utilise C# avec .NET Framework 3.5 SP1.ASP.NET: redimensionner (hauteur et largeur) une image téléchargée sur le serveur
Merci!
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; }
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 ;
}
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. –
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;
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. –
Et la taille du fichier? Je veux réduire la taille du fichier aussi. – VansFannel