gpt4 book ai didi

c# - 如何合成两个图像(使用 Source In 合成)?

转载 作者:太空宇宙 更新时间:2023-11-03 22:39:52 36 4
gpt4 key购买 nike

我需要执行 Source In composition在 2 张图片上。

例如这张图片:
Source
和蒙版图像(用黑色透明和黑白测试):
mask

应该产生结果:
result image

我正在尝试使用 ImageSharp 来做到这一点:

img.Mutate(imgMaskIn =>
{
using (var mask = Image.Load(maskImageFileName))
{
imgMaskIn.DrawImage(mask, new GraphicsOptions { AlphaCompositionMode = PixelAlphaCompositionMode.SrcIn});
}
});

但结果是蒙版图像。它应该基于 this merge request 工作.
是我错误地使用了库,还是存在错误?

在 ASP.NET Core 中还有其他方法可以做到这一点吗?

最佳答案

不幸的是,执行此操作的语法是 ImageSharp正在当前预览版和开发版之间变化,这应该是最终的 API。

1.0.0-beta0005 ,您可以像这样混合这些图像:

using (var pattern = Image.Load("img_pattern.png"))
using (var texture = Image.Load("img_texture.png"))
{
var options = new GraphicsOptions { BlenderMode = PixelBlenderMode.In };
using (var result = pattern.Clone(x => x.DrawImage(options, texture)))
{
result.Save("img_out.png");
}
}

请注意,您必须为此使用具有 alpha 透明度的图案图像。您不能使用键控透明度(至少不能使用此解决方案)。

为此,我已将图案设为透明(您可以 get the one I used here )并得到以下结果:

Result image


在最终版本中,它将如下所示:

using (var pattern = Image.Load("img_pattern.png"))
using (var texture = Image.Load("img_texture.png"))
{
var options = new GraphicsOptions { AlphaCompositionMode = PixelAlphaCompositionMode.SrcIn };
using (var result = pattern.Clone(x => x.DrawImage(texture, options)))
{
result.Save("img_out.png");
}
}

顺便说一句,这是解决这个问题的好方法。就是看PorterDuffCompositorTests file其中包含此功能的测试,因此将始终反射(reflect)当前的 API。

关于c# - 如何合成两个图像(使用 Source In 合成)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52875516/

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