ImageResizer - 支持maxWidth和MaxHeight - >的意思是保持纵横比

本文关键字:意思是 maxWidth 支持 MaxHeight ImageResizer | 更新日期: 2023-09-27 17:57:03

我正在寻找一个支持MaxWidth和MaxHeight的图像调整器...
我在哪里可以找到它?
下面的模块可以完成许多其他对我来说不需要的工作。
只想更改格式并支持最大宽度和最大高度。

图像调整器

ImageResizer - 支持maxWidth和MaxHeight - >的意思是保持纵横比

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

例如,假设您的图像尺寸为 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

库的功能是否超出您的需求并不重要。如果它执行您需要的操作,请使用它。额外的东西根本不会损害你。