简体   繁体   中英

Fill area outside the rotated image using ImageSharp

I am trying to draw two images on the other and when I rotate one of them, a black area appears around it在此处输入图像描述

        using Image punchImage = await Image.LoadAsync(Path.Combine(imgPath, img));
        using Image authorPfpImage = PathClass.GetAvatarImageFromUrlPath(author).Result;
        using Image victimPfpImage = PathClass.GetAvatarImageFromUrlPath(user).Result;

        authorPfpImage.Mutate(img => img
            .Resize(new Size(authorPfpImage.Width + sizeAdjust[0, 0], authorPfpImage.Height + sizeAdjust[0, 1]))
            .Rotate(rotationDegrees[0]));
        victimPfpImage.Mutate(img => img
            .Resize(new Size(victimPfpImage.Width + sizeAdjust[1, 0], victimPfpImage.Height + sizeAdjust[1, 1]))
            .Rotate(rotationDegrees[1])
            );

        using Image outputImage = punchImage.Clone(img => img
            .DrawImage(victimPfpImage, new Point(points[0], points[1]), 1f)
            .DrawImage(authorPfpImage, new Point(points[2], points[3]), 1f)
        );

        string savePath = Path.Combine(imgPath, img.First() + img);
        await outputImage.SaveAsync(savePath);

        return savePath;

Is there some universal solution for this in ImageSharp? Couldn't find any information about this.

This will be caused to do the fact your source images doesn't get a transparency channel in the fast Image.Load() loading path, instead you want to make sure you load images using a specific pixel format that support transparency. ie instead of calling Image.Load(fileSource) call Image.Load<Rgba32>(fileSource) instead.

Ideally you would update the PathClass.GetAvatarImageFromUrlPath(author) call to returns an Image<Rgba32> but if this is an external/third party component that you can't update you can instead clone it into a new Image<Rgba32> this is however at the expense of memory.

using Image<Rgba32> punchImage = await Image.LoadAsync<Rgba32>(Path.Combine(imgPath, img));
using Image authorPfpImageSource = PathClass.GetAvatarImageFromUrlPath(author).Result;
using Image authorPfpImage = authorPfpImageSource.CloneAs<Rgba32>(); // force this image to have an alpha/transparency channel
using Image victimPfpImageSource = PathClass.GetAvatarImageFromUrlPath(user).Result;
using Image victimPfpImage = victimPfpImageSource.CloneAs<Rgba32>(); // force this image to have an alpha/transparency channel

authorPfpImage.Mutate(img => img
    .Resize(new Size(authorPfpImage.Width + sizeAdjust[0, 0], authorPfpImage.Height + sizeAdjust[0, 1]))
    .Rotate(rotationDegrees[0]));
victimPfpImage.Mutate(img => img
    .Resize(new Size(victimPfpImage.Width + sizeAdjust[1, 0], victimPfpImage.Height + sizeAdjust[1, 1]))
    .Rotate(rotationDegrees[1])
    );

punchImage.Mutate(img => img
    .DrawImage(victimPfpImage, new Point(points[0], points[1]), 1f)
    .DrawImage(authorPfpImage, new Point(points[2], points[3]), 1f)
);

string savePath = Path.Combine(imgPath, img.First() + img);
await punchImage.SaveAsync(savePath);

return savePath;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM