我正在寻找像下面这样支持 MaxWidth 和 MaxHeight 的 ImageResizer ...
我在哪里可以找到它?
下面的模块做了许多其他我不需要的工作。
只想更改格式并支持 maxwidth 和 maxheight。
问问题
1269 次
2 回答
2
您可以编写一个强制最大宽度和最大高度的包装器,并保持纵横比。
例如,假设您有一张 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
于 2011-05-19T15:22:57.663 回答
1
图书馆是否比您需要的更多并不重要。如果它可以满足您的需求,请使用它。额外的东西根本不会损害你。
于 2011-05-19T15:21:26.807 回答