图像在C#中调整大小 - 确定调整大小尺寸(高度和宽度)的算法 [英] Image Resize in C# - Algorith to determine resize dimensions (height and width)

查看:492
本文介绍了图像在C#中调整大小 - 确定调整大小尺寸(高度和宽度)的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要缩小高度或宽度大于预定义像素值的图像。

I need to scale down an image that has a height or width greater than a predefined pixel value.

我写了一些代码来查看原始图像,检查宽度,高度或高度和宽度是否大于最大宽度/最大高度设置。

I wrote some code that takes a look at the original image, checks to see if either the width, the height, or the height and width are greater than the Max Width/Max Height settings.

我现在需要弄清楚是什么要根据后一个值的最大值调整大小的维度。

I now need to figure out what dimensions to resize to based on the max of the latter value.

例如:如果图像 900h x 300w 并且MAX高度 700h 我需要将高度调整为 700 并将宽度调整为 ???? < - 这是我需要计算的..

For example: If the image is 900h x 300w and the MAX Height is 700h I will need to resize the height to 700 and the width to ???? <-- this is what I need to calculate..

创建和保存图像文件很简单,超出此帖子的范围:

Creating and saving the image file is simple, and outside the scope of this post:

// 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;
}

创建缩略图:
这就是我现在正在工作的。 ..不完整:

Create thumbnail: This is what I'm working now... not complete:

if (doResize)
{
    ImageUtilities.ResizeImage(image, resizeWidth, resizeHeight);
}


推荐答案

以下是两种制作方法这个计算。根据您对问题的看法,可能看起来比另一个更直观。它们在数学上等效于几个小数位。

Here are two ways to make this calculation. Depending upon how you think about the problem, one may seem more intuitive than the other. They are mathematically equivalent to several decimal places.

两者对Math.Round都是安全的,但只有ConstrainVerbose产生的结果总是小于maxWidth / maxHeight。

Both are safe for Math.Round, but only ConstrainVerbose produces results that are always less than maxWidth/maxHeight.

SizeF ConstrainConcise(int imageWidth, int imageHeight, int maxWidth, int maxHeight){
    // Downscale by the smallest ratio (never upscale)
    var scale = Math.Min(1, Math.Min(maxWidth / (float)imageWidth, maxHeight / (float) imageHeight));
    return new SizeF(scale * imageWidth, scale * imageHeight);
}

SizeF ConstrainVerbose(int imageWidth, int imageHeight, int maxWidth, int maxHeight){
    // Coalculate the aspect ratios of the image and bounding box
    var maxAspect = (float) maxWidth / (float) maxHeight;
    var aspect =  (float) imageWidth / (float) imageHeight;
    // Bounding box aspect is narrower
    if (maxAspect <= aspect && imageWidth > maxWidth)
    {
        // Use the width bound and calculate the height
        return new SizeF(maxWidth, Math.Min(maxHeight, maxWidth / aspect));
    }
    else if (maxAspect > aspect && imageHeight > maxHeight)
    {
        // Use the height bound and calculate the width
        return new SizeF(Math.Min(maxWidth, maxHeight * aspect), maxHeight);
    }else{
        return new SizeF(imageWidth, imageHeight);
    }
}

这里的暴力单位测试

这篇关于图像在C#中调整大小 - 确定调整大小尺寸(高度和宽度)的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆