c# - 缩放图像,但保持其比例

标签 c# asp.net image-processing image-scaling

我想缩放图像,但我不希望图像看起来歪斜。

图片必须为 115x115(长 x 宽)。

图片不能超过 115 像素高(长度),但如果需要,宽度可以小于 115 但不能超过。

这很棘手吗?

最佳答案

您需要保持纵横比:

float scale = 0.0;

    if (newWidth > maxWidth || newHeight > maxHeight)
    {
        if (maxWidth/newWidth < maxHeight/newHeight)
        {
            scale = maxWidth/newWidth;
        }
        else
        {
            scale = maxHeight/newHeight;
        }
        newWidth = newWidth*scale;
        newHeight = newHeight*scale;

    }

在代码中,最初的newWidth/newHeight是图片的宽/高。

关于c# - 缩放图像,但保持其比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1979958/

相关文章:

c++ - 用于极端图像缩小的快速、高质量像素插值

python - Scikit 图片 : proper way of counting cells in the objects of an image

c# - 部署应用程序时,asp.net mvc 应用程序抛出 Controller 异常

c# - 如何通过 Selenium Script C# 将参数传递给 chrome 扩展?

c# - 使用MinHash查找2张图像之间的相似性

c# - 在 MVC 中处理具有相同名称的多个操作

c# - Windows文件夹路径困惑

c# - 如何在 C# 中使用 GetNextWindow()?

c# - CLR 工作线程和工作线程有什么区别?

asp.net - 同一 Web 服务器上跨应用程序的身份验证/登录问题 | IIS 7.5 安装