簡體   English   中英

SixLabors ImageSharp 裁剪調整錯誤的寬度和高度

[英]SixLabors ImageSharp crop-resizes wrong width and height

我使用 SixLabors.ImageSharp 使用以下代碼調整照片大小:

https://www.nuget.org/packages/SixLabors.ImageSharp/
ASP .NET 核心 5.0

輸入照片:720x960px
期望輸出:248x186px
實際結果:186x248px

為什么寬度變成高度,反之亦然?

using (var stream = new MemoryStream())
{
    using var image = Image.Load(bytes);
    image.Mutate(x => x
        .Resize(new ResizeOptions
            {
                Mode = ResizeMode.Crop,
                Position = AnchorPositionMode.Center,
                Size = new Size(248, 186)
            })
        .BackgroundColor(Color.White));

    await image.SaveAsync(stream, new JpegEncoder() { Quality = 85 });
}

這是因為您的源照片有一個元數據標志,該標志具有自定義方向集,這會導致客戶端在渲染/解釋時旋轉圖像,但實際上像素數據實際上比它顯示的旋轉了 90 度。

您將需要使用.AutoOrient() api 來為您解決這個問題,這會強制像素數據正確定向到所需的方向。

using (var stream = new MemoryStream())
{
    using var image = Image.Load(bytes);
    image.Mutate(x => x
        .AutoOrient() // this is the important thing that needed adding
        .Resize(new ResizeOptions
            {
                Mode = ResizeMode.Crop,
                Position = AnchorPositionMode.Center,
                Size = new Size(248, 186)
            })
        .BackgroundColor(Color.White));

    await image.SaveAsync(stream, new JpegEncoder() { Quality = 85 });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM