And*_*rew 0 c# image image-processing winforms image-resizing
我正在为我的网站做一些工作,我想自动调整我的图像..但不仅自动调整大小,而且保持它们成比例,即使我调整其宽度或高度.我想添加额外的白色边框以补偿新的空间.
我以前从未做过任何形象工作,我应该怎么做呢?
计算高度,就像宽度适合一样,然后根据容器的高度进行检查.如果它更高,则计算宽度以使高度合适:
newHeight = oldHeight * containerWidth / oldWidth;
if (newHeight <= containerHeight) {
newWidth = containerWidth;
} else {
newWidth = oldWidth * containerHeight / oldHeight;
newHeight = containerHeight;
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以计算图像的放置位置:
x = (containerWidth - newWidth) / 2;
y = (containerHeight - newHeight) / 2;
Run Code Online (Sandbox Code Playgroud)