c# - 如何合并两张图片?

标签 c# .net-core imagesharp

将 ImageSharp 用于 .Net 核心,如何并排组合 2 个图像?例如:使 2 100x150px 变成 1 100x300px(或 200x150px)

最佳答案

您可以使用此代码将 2 个源图像绘制到正确尺寸的新图像上。

它需要您的 2 个源图像,将它们调整到所需的确切尺寸,然后将它们中的每一个绘制到准备保存的第三个图像上。

using (Image<Rgba32> img1 = Image.Load<Rgba32>("source1.png")) // load up source images
using (Image<Rgba32> img2 = Image.Load<Rgba32>("source2.png"))
using (Image<Rgba32> outputImage = new Image<Rgba32>(200, 150)) // create output image of the correct dimensions
{
    // reduce source images to correct dimensions
    // skip if already correct size
    // if you need to use source images else where use Clone and take the result instead
    img1.Mutate(o => o.Resize(new Size(100, 150))); 
    img2.Mutate(o => o.Resize(new Size(100, 150)));

    // take the 2 source images and draw them onto the image
    outputImage.Mutate(o => o
        .DrawImage(img1, new Point(0, 0), 1f) // draw the first one top left
        .DrawImage(img2, new Point(100, 0), 1f) // draw the second next to it
    );

    outputImage.Save("ouput.png");
}

此代码假定您在范围内具有这些用途
using SixLabors.ImageSharp.Processing.Transforms;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing.Drawing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;

关于c# - 如何合并两张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50860392/

相关文章:

c# - 从集线器外部广播的 SignalR 不起作用

c# - 如何在没有死锁的情况下为一个简单的操作阻塞 EF6.1 中的表?

c# - BeforeBuild 事件未触发

c# - 在 C# 中使用 SixLabors.ImageSharp 裁剪和移动图片

c# - 获取 '' 缺少 SOI 标记。来自 JpegDecoderr

c# - 测试 HtmlHelper 时如何解决图像路径问题?

c# - 在 Entity Framework Core 3 中使用 TransactionScope 时 SET IDENTITY_INSERT 不起作用

postgresql - F# 连接 SQLProvider 与 Postgres

c# - 使用 C# .NET Core 1.1 进行 Xml 数字签名

c# - ImageSharp 在 Core 2.2 中的图像上绘制形状