C#.NET - 将多个 .png 合并到一个位图中 (.bmp)

问题描述 投票:0回答:1

我在一个文件夹中存储了多个 images.png (带有名称)。 从代码(C#)中,我想创建一个矩形位图(带有透明背景而不是白色),其中嵌入了所有图像。 像这样的东西:

enter image description here

我该怎么做?

c# .net image bitmap png
1个回答
0
投票

如果您使用 .NET Framework,我建议您看看 GDI+。它可以让您轻松绘制形状和其他图像,无需太多工作:

// Create an image of your desired size
var bitmap = new Bitmap(1000, 1000);
using (var g = Graphics.FromImage(bitmap))
{
    // Load another image and draw it at a certain position
    var otherImage = Bitmap.FromFile(@"otherimage.png");
    g.DrawImage(otherImage, new Point(60, 10));

    g.Save();
}

bitmap.Save(@"output.bmp");

如果您使用 .NET,您仍然可以通过引用

Bitmap
来使用
System.Drawing.Common
,但您不会获得 GDI+。这使得操作变得有点困难,因为默认情况下你只有
GetPixel
/
SetPixel
。因此,基本的复制很容易实现,但与拉伸、绘制线条等相关的任何内容都可以。如果您想这样做,您应该寻找 .NET 的图像处理库。

对于你所说的情况,似乎简单的复制就足够了:

var bitmap = new Bitmap(1000, 1000);
var otherImage = (Bitmap)Image.FromFile(@"otherImage.png");
CopyTo(otherImage, bitmap, 60, 10);

void CopyTo(Bitmap source, Bitmap destination, int offsetX, int offsetY)
{
    for (var x = 0; x < source.Width; x++)
    for (var y = 0; y < source.Height; y++)
    {
        destination.SetPixel(x + offsetX, y + offsetY, source.GetPixel(x, y));
    }
}

bitmap.Save(@"output.bmp");

无论哪种方式,您都需要进行一些布局来确定最终图像的正确尺寸并正确定位图像,但这应该可以帮助您入门。

© www.soinside.com 2019 - 2024. All rights reserved.