C# 如何合成两幅图像(在合成中使用源)?

C# 如何合成两幅图像(在合成中使用源)?,c#,imagesharp,C#,Imagesharp,我需要对2个图像执行 例如,此图像: 以及使用黑色透明和黑白测试的遮罩图像: 应产生以下结果: 我正试图通过以下方式做到这一点: 但结果是蒙版图像。它应该基于。 我是否错误地使用了图书馆,还是有错误 在ASP.NET Core中是否有其他方法可以做到这一点?不幸的是,用于执行此操作的语法在当前预览版本和开发版本之间发生变化,开发版本应该是此操作的最终API 使用,可以如下方式混合这些图像: using (var pattern = Image.Load("img_pattern.png"))

我需要对2个图像执行

例如,此图像: 以及使用黑色透明和黑白测试的遮罩图像:

应产生以下结果:

我正试图通过以下方式做到这一点:

但结果是蒙版图像。它应该基于。 我是否错误地使用了图书馆,还是有错误


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

不幸的是,用于执行此操作的语法在当前预览版本和开发版本之间发生变化,开发版本应该是此操作的最终API

使用,可以如下方式混合这些图像:

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");
    }
}
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");
    }
}
请注意,为此,必须使用具有alpha透明度的图案图像。您不能使用键控透明度,至少不能与此解决方案一起使用

为此,我已使图案透明,您可以获得以下结果:

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

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");
    }
}
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");
    }
}

顺便说一句,了解这一点的一个好方法是查看包含此功能测试的,因此将始终反映当前API。

不幸的是,执行此操作的语法在当前预览版本和开发版本之间发生变化,开发版本应该是此功能的最终API

使用,可以如下方式混合这些图像:

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");
    }
}
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");
    }
}
请注意,为此,必须使用具有alpha透明度的图案图像。您不能使用键控透明度,至少不能与此解决方案一起使用

为此,我已使图案透明,您可以获得以下结果:

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

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");
    }
}
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");
    }
}

顺便说一句,了解这一点的一个好方法是查看包含此功能测试的,因此将始终反映当前API。

我正在删除ASP.NET核心标记,因为这与此无关。你只想用ImageSharp在.NET中解决这个问题。一旦你能做到这一点,你就可以直接在ASP.NET内核中使用这个解决方案了。更多信息。我正在删除ASP.NET核心标记,因为这与此无关。你只想用ImageSharp在.NET中解决这个问题。一旦你能做到这一点,你就可以直接在ASP.NET内核中使用这个解决方案了。更多信息。开发版本包含最终的Porter Duff API,具有可分离的alpha和颜色混合模式。@JamesSouth哦,很高兴知道,我将更新我的答案,以包括最终的API:从1.0.0-dev002031开始,这将开始工作。请记住,如果输入文件为1bbp,则库将尝试以1bps的速度将其保存,否则在保存时需要使用新的PngEncoder实例。开发版本包含最终的Porter Duff API,具有可分离的alpha和颜色混合模式。@JamesSouth哦,很高兴知道,我将更新我的答案,以包括最终的API:从1.0.0-dev002031开始,现在可以使用了。请记住,如果输入文件为1bbp,则库将尝试以1bps的速度将其保存,否则在保存时需要使用新的PngEncoder实例。