【问题标题】:ImageResizer - that supports maxWidth & MaxHeight -> mean keeping aspect RatioImageResizer - 支持 maxWidth & MaxHeight -> 表示保持纵横比
【发布时间】:2014-02-21 02:02:07
【问题描述】:

我正在寻找像下面这样支持 MaxWidth 和 MaxHeight 的 ImageResizer ...
我在哪里可以找到它?
下面的模块做了许多我不需要的其他工作。
只想更改格式并支持 maxwidth 和 maxheight。

ImageResizer

【问题讨论】:

标签: c# asp.net image-resizing aspect-ratio


【解决方案1】:

您可以编写一个强制最大宽度和最大高度并保持纵横比的包装器。

例如,假设您有一张 640 x 120 的图片,并且您的最大值为 1,920 x 1,440。现在,您想让该图像尽可能大,因此您编写:

ResizeImage(image, 1920, 1440)

如果你这样做,纵横比就会被拍摄。

您需要计算现有图像的纵横比并调整值。

// Compute existing aspect ratio
double aspectRatio = (double)image.Width / image.Height;

// Clip the desired values to the maximums
desiredHeight = Math.Min(desiredHeight, MaxHeight);
desiredWidth = Math.Min(desiredWidth, MaxWidth);

// This is the aspect ratio if you used the desired values.
double newAspect = (double)desiredWidth / desiredHeight;

if (newAspect > aspectRatio)
{
    // The new aspect ratio would make the image too tall.
    // Need to adjust the height.
    desiredHeight = (int)(desiredWidth / aspectRatio);
}
else if (newAspect < aspectRatio)
{
    // The new aspect ratio would make the image too wide.
    // Need to adjust the width.
    desiredWidth = (int)(desiredHeight * aspectRatio);
}

// You can now resize the image using desiredWidth and desiredHeight

【讨论】:

  • 嗯。我是不是搞反了?
【解决方案2】:

库是否超出您的需要并不重要。如果它可以满足您的需求,请使用它。额外的东西根本不会损害你。

【讨论】:

  • 该模块的版本 3 具有插件架构,因此默认情况下它仅提供调整大小和格式转换,以及其他一些小功能。额外的东西是通过 36 多个插件添加的。它现在也是免费的,您可以从imageresizing.net/download 下载源代码。
猜你喜欢
  • 2012-08-25
  • 2018-04-02
  • 2018-03-11
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多