Je développe un site web en utilisant ASP.net. Là, les utilisateurs peuvent télécharger des images. Lorsque je sauvegarde des images, j'enregistre une image de petite taille (Miniature). Pour ce faire, j'utiliser ce codeComment enregistrer une image miniature tout en conservant le bon format d'image?
public void SaveThumbnailImage(Stream sourcePath, int width, int height, string imageFileName)
{
using (var image = System.Drawing.Image.FromStream(sourcePath))
{
//a holder for the result
Bitmap result = new Bitmap(width, height);
//set the resolutions the same to avoid cropping due to resolution differences
result.SetResolution(image.HorizontalResolution, image.VerticalResolution);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
//draw the image into the target bitmap
graphics.DrawImage(image, 0, 0, result.Width, result.Height);
}
thumbnailfullImagePath = string.Format("/Images/thumbnail/{0}", imageFileName);
result.Save(Server.MapPath(thumbnailfullImagePath), image.RawFormat);
}
}
hauteur: 105 largeur est de 150
Au-dessus de cordon fonctionne bien pour les photos de type paysage. Mais si je télécharge un portrait, il ne conserve pas ses résolutions correctes. Alors, comment optimiser le code ci-dessus pour enregistrer la vignette tout en conservant son rapport largeur/hauteur d'origine?