Cambiar tamaño de imagen en C # - Algorith para determinar las dimensiones de cambio de tamaño (alto y ancho)
Necesito reducir la escala de una imagen que tiene una altura o ancho mayor que un valor de píxel predefinido.
Escribí un código que echa un vistazo a la imagen original, verifica si el ancho, la altura, o la altura y el ancho son mayores que la configuración Ancho máximo / Altura máxima.
Ahora necesito averiguar a qué dimensiones cambiar el tamaño en función del máximo del último valor.
Por ejemplo: si la imagen es900h x 300w
y la Altura MÁXIMA es700h
Necesitaré cambiar el tamaño de la altura a700
y el ancho a????
<- esto es lo que necesito calcular ...
Crear y guardar el archivo de imagen es simple y está fuera del alcance de esta publicación:
// First I get the max height and width allowed:
int resizeMaxHeight = int.Parse(Utility.GetConfigValue("ResizeMaxHeight")); // in config: 700px
int resizeMaxWidth = int.Parse(Utility.GetConfigValue("ResizeMaxWidth")); // in config: 500px
// Save original:
try
{
filebase.SaveAs(savedFileName);
}
catch (System.IO.DirectoryNotFoundException ex)
{
Logger.Instance.LogException(ex, 0, "FileTransfer");
}
// Determin original dimensions:
Image image = System.Drawing.Image.FromFile(Server.MapPath(savedFileName));
int resizeHeight, resizeWidth;
bool doResize = true;
// both height and width are greater than the allowed height and width:
if (image.Width > resizeMaxWidth && image.Height > resizeMaxHeight)
{
if (image.Height > image.Width)
resizeHeight = resizeMaxHeight;
else
resizeWidth = resizeMaxWidth;
}
else if (image.Width > resizeMaxWidth)
{
// width is too great, but height is ok
resizeWidth = resizeMaxWidth;
}
else if (image.Height > resizeMaxHeight)
{
// height is too great, but width is ok
resizeHeight = resizeMaxHeight;
}
else
{
// image is ok size, don't resize:
doResize = false;
}
Crear miniatura: Esto es lo que estoy trabajando ahora ... no completo:
if (doResize)
{
ImageUtilities.ResizeImage(image, resizeWidth, resizeHeight);
}