【发布时间】:2011-07-10 12:17:14
【问题描述】:
我需要缩小高度或宽度大于预定义像素值的图像。
我编写了一些代码来查看原始图像,检查宽度、高度或高度和宽度是否大于最大宽度/最大高度设置。
我现在需要根据后一个值的最大值确定要调整到哪些尺寸。
例如:如果图像为900h x 300w,最大高度为700h,我需要将高度调整为700,将宽度调整为????
创建和保存图像文件很简单,超出了本文的范围:
// 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;
}
创建缩略图: 这就是我现在正在做的……不完整:
if (doResize)
{
ImageUtilities.ResizeImage(image, resizeWidth, resizeHeight);
}
【问题讨论】:
标签: c# image-processing bitmap image-manipulation system.drawing