.Net核心结合多个PNG

时间:2019-02-15 15:50:25

标签: .net .net-core

我只是试用一些.Net内核,并检查如何将某些PNG合并为一个,但似乎有一个问题,即仅显示最后加载的图像(其余图像仅以纯白色显示)。

因此,我正在尝试合并:

但是得到:

透明度非常重要,这很正常。

这是Mac上的.Net Core 3

  int targetHeight = 1051; //height and width of the finished image
    int targetWidth = 1089;

    //be sure to use a pixelformat that supports transparency
    using (var bitmap = new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppArgb))
    {
        bitmap.MakeTransparent();

        using (var canvas = Graphics.FromImage(bitmap))
        {
            canvas.Clear(Color.Transparent);

            var bm1 = new Bitmap("./components/1.png");
            canvas.DrawImage(bm1, new Point(0, 0));

            var bm7 = new Bitmap("./components/7.png");
            canvas.DrawImage(bm7, new Point(0, 0));

            canvas.Save();
        }

        bitmap.Save("ouput.png", ImageFormat.Png);
    }

1 个答案:

答案 0 :(得分:1)

我无法在.NET Core 3 Mac上进行测试,但是在Windows上使用.NET Core 2可以运行:

Nuget软件包:System.Drawing.Common,4.5.1

namespace ConsoleApp20
{
    using System.Drawing;
    using System.Drawing.Imaging;

    class Program
    {
        static void Main(string[] args)
        {
            var head = new Bitmap("./head.png");
            var body = new Bitmap("./body.png");

            using (var bitmap = new Bitmap(body))
            {
                bitmap.MakeTransparent();

                using (var canvas = Graphics.FromImage(bitmap))
                {
                    canvas.DrawImage(head, new Point());
                    canvas.Save();
                }

                bitmap.Save("./output.png", ImageFormat.Png);
            }
        }
    }
}

enter image description here