gpt4 book ai didi

c# - 最佳调整大小和/或裁剪逻辑

转载 作者:太空狗 更新时间:2023-10-29 21:48:00 26 4
gpt4 key购买 nike

我已经遇到过几次了,觉得把它放在那里会很好。您最好的图像调整大小和/或裁剪逻辑是什么。这个想法是用目标图像、尺寸和裁剪标志调用某些方法 - 这将返回或保存或任何所需的图像。

我的在下面。从 VB 转换为 C#,所以是的,会有小错误,但我们正在关注的是逻辑。

// INIT
// On/off
bool WeAreCropping = true;

// Get some dimensions
int TargetWidth = RequestedWidth;
int TargetHeight = RequestedHeight;
int SourceWidth = SourceImage.Width;
int SourceHeight = SourceImage.Height;
int ResizeWidth = TargetWidth;
int ResizeHeight = TargetHeight;

// GET RESIZE VALUES
// Are we cropping?
if (WeAreCropping) {

// Get source and target aspect ratio
double SourceAspectRatio = SourceWidth / SourceHeight;
double TargetAspectRatio = TargetWidth / TargetHeight;

// Compare aspect ratios to find out if we should we resize by
// width or height or nothing
if (TargetAspectRatio < SourceAspectRatio) {
ResizeWidth = TargetHeight / SourceHeight * SourceWidth;
}
else if (TargetAspectRatio > SourceAspectRatio) {
ResizeHeight = TargetWidth / SourceWidth * SourceHeight;
}
else {
// Same aspect ratio
}


}
else {

// If the target image is bigger than the source
if (TargetWidth > SourceWidth && TargetHeight > SourceHeight) {
TargetWidth = SourceWidth;
TargetHeight = SourceHeight;
}

double Ratio = 0;

// What ratio should we resize it by
if (SourceWidth / TargetWidth > SourceHeight / TargetHeight) {
Ratio = SourceWidth / TargetWidth;
}
else {
Ratio = SourceHeight / TargetHeight;
}

ResizeWidth = Math.Ceiling(SourceWidth / Ratio);

ResizeHeight = Math.Ceiling(SourceHeight / Ratio);
}

// TIME TO DO SUMFINK
// Resize the image using ResizeWidth and ResizeHeight
// Do it

if (WeAreCropping) {
// Crop the resized image at the center TargetWidth and TargetHeight
// Do it
}

我怀疑这可能会更优雅,所以也许你可以做得更好。

最佳答案

我认为您应该使用标准几何类型,例如 RectangleSize。那么您可能还应该支持一个用例,当调用者想要将图像放入某个较大的矩形中,但仍希望保持原始图像大小比例时。

您可以找到一种实现调整大小的方法 here .

关于c# - 最佳调整大小和/或裁剪逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1626355/

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