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

发布于 2024-08-09 09:40:51 字数 1885 浏览 7 评论 0原文

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

我的在下面。从 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
}

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

I've come across this a few times and thought it would be good to put it out there. What's your best image resize and/or crop logic. The idea being that some method is called with a target image, dimensions and a crop flag - this would return or save off or whatever the desired image.

Mine is below. Converted from VB to C# so yes there'll be small bugs but logic is what we're looking at.

// 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
}

I suspect this could be much more elegant so maybe you could do better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢华的一滴泪 2024-08-16 09:40:51

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

您可以在 此处找到实现调整大小的一种方法

I'd say you should utilize standard geometry types like Rectangle and Size. Then you should probably also support a use case, when the caller wants to fit the image in some larger rectangle, but still wants to keep the original image sizes ratio.

One way to implement the resizing you can find here.

娇妻 2024-08-16 09:40:51

这可能比您需要的要复杂一些,但是对于一些高级裁剪技术的示例,看看这个

This is probably a bit more involved than what you need, but for an example of some advanced cropping techniques, check this out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文