Sur la base de la réponse de Brij J'ai fait cette méthode d'extension:
/// <summary>
/// Resize image to max dimensions
/// </summary>
/// <param name="img">Current Image</param>
/// <param name="maxWidth">Max width</param>
/// <param name="maxHeight">Max height</param>
/// <returns>Scaled image</returns>
public static Image Scale(this Image img, int maxWidth, int maxHeight)
{
double scale = 1;
if (img.Width > maxWidth || img.Height > maxHeight)
{
double scaleW, scaleH;
scaleW = maxWidth/(double)img.Width;
scaleH = maxHeight/(double)img.Height;
scale = scaleW < scaleH ? scaleW : scaleH;
}
return img.Resize((int)(img.Width * scale), (int)(img.Height * scale));
}
/// <summary>
/// Resize image to max dimensions
/// </summary>
/// <param name="img">Current Image</param>
/// <param name="maxDimensions">Max image size</param>
/// <returns>Scaled image</returns>
public static Image Scale(this Image img, Size maxDimensions)
{
return img.Scale(maxDimensions.Width, maxDimensions.Height);
}
La méthode de modification de taille:
/// <summary>
/// Resize the image to the given Size
/// </summary>
/// <param name="img">Current Image</param>
/// <param name="width">Width size</param>
/// <param name="height">Height size</param>
/// <returns>Resized Image</returns>
public static Image Resize(this Image img, int width, int height)
{
return img.GetThumbnailImage(width, height, null, IntPtr.Zero);
}
Cela semble similaire à http://stackoverflow.com/questions/2823200/. Voyez ma réponse à cette question. –