gpt4 book ai didi

c# - WPF的 "Stretch.Uniform"等图片FILL逻辑

转载 作者:行者123 更新时间:2023-11-30 22:54:37 24 4
gpt4 key购买 nike

我正在寻找要在我的控制台应用程序中应用的图像填充逻辑,它会生成 TIFF 文件。我的图像容器尺寸为 1200 * 1800 像素。

已应用以下逻辑来调整图像大小以适应此尺寸,但此逻辑应用 FIT 部分而不是 FILL 部分。

如果我的图像尺寸为 1200 * 1600 那么我会在下方出现空白区域,如果我尝试在容器中调整图像大小,那么它会拉伸(stretch)图像并且图像看起来不太好. 1200 * 1600 上面的图片非常适合。

我正在寻找类似于 WPF 应用程序的“Stretch.Uniform”的逻辑,它在不丢失图像实际 View 的情况下统一填充图像。

private static Dimension getScaledDimension(Dimension imgSize, Dimension boundary)
{

int original_width = imgSize.width;
int original_height = imgSize.height;
int bound_width = boundary.width;
int bound_height = boundary.height;
int new_width = original_width;
int new_height = original_height;

// first check if we need to scale width.
if (original_width > bound_width)
{
//scale width to fit
new_width = bound_width;
//scale height to maintain aspect ratio
new_height = (new_width * original_height) / original_width;
}


// then check if we need to scale even with the new height.
if (new_height > bound_height)
{
//scale height to fit instead
new_height = bound_height;
//scale width to maintain aspect ratio
new_width = (new_height * original_width) / original_height;
}

return new Dimension { height = new_height, width = new_width };
}

最佳答案

Principle image

检查 2 个矩形的纵横比以决定适合哪个属性。

private static Dimension getScaledDimension(Dimension imgSize, Dimension boundary)
{
int original_width = imgSize.width;
int original_height = imgSize.height;
int bound_width = boundary.width;
int bound_height = boundary.height;

double original_aspect = (double)original_width / original_height;
double bound_aspect = (double)bound_width / bound_height;

//original size is wider than boundary, fit boundary width
if(original_aspect > bound_aspect)
{
return new Dimension
{
width = boundary.width,
height = (int)Math.Round(boundary.width / original_aspect)
};
}

//original size is taller than boundary, fit boundary height
else
{
return new Dimension
{
height = boundary.height,
width = (int)Math.Round(boundary.height * original_aspect)
};
}
}

关于c# - WPF的 "Stretch.Uniform"等图片FILL逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56076613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com