简体   繁体   English

在保持纵横比和舍入到整数的同时调整大小

[英]Resizing while maintaining aspect ratio and rounding to even integers

I am allowing the user to resize an image while trying to maintain the original aspect ratio of the image. 我允许用户在尝试保持图像的原始宽高比时调整图像大小。

For each resize operation I have an "offset" variable which indicates the width and height change. 对于每个调整大小操作,我有一个“偏移”变量,表示宽度和高度的变化。 This is based on the mouse movement, so it could be any combination of values depending on how much they moved the mouse while resizing. 这是基于鼠标移动,因此它可以是值的任意组合,具体取决于它们在调整大小时移动鼠标的程度。

What I am doing now is taking the larger of the two values (x and y change) and using that to calculate the other value at the same aspect ratio. 我现在正在做的是取两个值中的较大值(x和y变化)并使用它来计算相同宽高比的另一个值。 Here is my code: 这是我的代码:

if (Math.Abs(offset.X) > Math.Abs(offset.Y))
{
    offset.Y = (int)(offset.X / AspectRatio);
}
else
{
    offset.X = (int)(offset.Y * AspectRatio);
}

Aspect ratio is the standard width/height value. 宽高比是标准宽度/高度值。

The problem with my code is that it's using integer values so it's rounding and causing the aspect ratio to warp. 我的代码的问题是它使用整数值,因此它会舍入并导致宽高比变形。

I assume what I need to do is snap to integer values that are evenly divisible by the aspect ratio, or something to that end. 我假设我需要做的是捕捉到可以被宽高比整除的整数值,或者为此目的。 But I don't know how to do it by modifying these x and y "offset" values. 但我不知道如何通过修改这些x和y“偏移”值来做到这一点。

Make offset.X,.Y and AspectRatio floats/doubles, do whatever calculations you need, and only once new sizes are calculated round it to integer. 使offset.X,.Y和AspectRatio浮动/加倍,执行您需要的任何计算,并且只计算一次新的大小将其四舍五入为整数。
This way you'll have maximum precision possible. 这样你就可以获得最大的精度。

PS Use Math.Round instead of simply casting to int, this will gain you some extra precision. PS使用Math.Round而不是简单地转换为int,这将获得一些额外的精度。

I assume what I need to do is snap to integer values that are evenly divisible by the aspect ratio, or something to that end. 我假设我需要做的是捕捉到可以被宽高比整除的整数值,或者为此目的。 But I don't know how to do it by modifying these x and y "offset" values. 但我不知道如何通过修改这些x和y“偏移”值来做到这一点。

Either you will have an imperfect rounding of the aspect ratio, cropping, or chunky resizes. 要么你有不完美的纵横比,裁剪或厚实的调整。 Think of this: 想一想:

With a 3:4 aspect ratio, what is the minimum size you can scale horizontally and still perfectly match the original ratio? 使用3:4纵横比,您可以水平缩放的最小尺寸是多少,仍然与原始比例完美匹配?

Four pixels. 四个像素。

Now change your aspect ratio to two large prime numbers and you'll see the problem. 现在将纵横比更改为两个大素数,您将看到问题所在。

Instead of this, you can either crop the image in one dimension, or do interpolation using image filtering, and live with the not-quite-perfect aspect ratio change. 除此之外,您可以在一个维度上裁剪图像,或使用图像过滤进行插值,并使用不太完美的宽高比更改。

Make sure your calculations are correct, tho. 确保你的计算是正确的。 Calculate your aspect ratio as a floating point value, and truncate to an integer only at the final calculation. 将纵横比计算为浮点值,并仅在最终计算时截断为整数。 Otherwise you will have double rounding errors. 否则您将出现双舍入错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在保持宽高比的同时调整图像大小 - Resize Image while maintaining aspect ratio 在保持纵横比的同时用鼠标调整框大小? - Resize a box with the mouse while maintaining the aspect ratio? 将PictureBox中的图像调整到最大可能的大小,同时保持纵横比? - Resize an image in a PictureBox to as large as it can go, while maintaining aspect ratio? 如何在保持其纵横比的同时调整PictureBox的大小 - How to resize a PictureBox while maintaining its Aspect Ratio c# 将图像调整为不同的大小,同时保持纵横比 - c# Image resizing to different size while preserving aspect ratio 将 android 应用程序构建为一个统一的独立应用程序(在 PC 上使用),同时保持正确的纵横比 - Build android app as a unity stand-alone (for use on a PC) while maintaining correct aspect ratio 如何在保持纵横比和居中的同时使我的XNA游戏全屏? - How do I make my XNA game fullscreen while maintaining aspect ratio and centered? 将 System.Drawing.Bitmap 缩放到给定大小,同时保持纵横比 - Scaling a System.Drawing.Bitmap to a given size while maintaining aspect ratio 尝试调整大小和压缩高度,仅保持高品质的宽高比 - Trying to resize and compress height only maintaining aspect ratio with good quality Unity在3D空间中显示图像并保持宽高比 - Unity Showing image in 3D space with maintaining aspect ratio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM