如何组合两个图像?

时间:2018-06-14 14:55:10

标签: c# .net-core imagesharp

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

1 个答案:

答案 0 :(得分:1)

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

它需要您的2个源图像,将它们调整到所需的精确尺寸,然后将它们绘制到第三张图像上以备保存。

using (Image<Rgba32> img1 = Image.Load("source1.png")) // load up source images
using (Image<Rgba32> img2 = Image.Load("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, 1f, new Point(0, 0)) // draw the first one top left
        .DrawImage(img2, 1f, new Point(100, 0)) // 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;