C#.NET实现多PNG图片合并为带透明背景的Bitmap(BMP)
实现步骤与代码示例
核心思路
- 遍历目标文件夹,筛选出所有PNG图片文件
- 创建带透明通道的目标Bitmap(使用
PixelFormat.Format32bppArgb确保透明背景生效) - 通过
Graphics类将每张PNG绘制到目标Bitmap的指定位置,保留原图片的透明效果
代码实现
情况1:横向平铺所有图片
这种方式会将所有图片按顺序横向排列,目标Bitmap的高度取所有图片中的最大高度,宽度为所有图片宽度之和。
using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Collections.Generic; class ImageCombiner { static void Main(string[] args) { string imageFolder = @"C:\YourImageFolderPath"; // 替换为你的图片文件夹路径 string outputPath = @"C:\CombinedImage.png"; // 获取文件夹内所有PNG文件 string[] pngFiles = Directory.GetFiles(imageFolder, "*.png", SearchOption.TopDirectoryOnly); if (pngFiles.Length == 0) return; // 预加载所有图片,获取尺寸信息 List<Bitmap> images = new List<Bitmap>(); int totalWidth = 0; int maxHeight = 0; foreach (string filePath in pngFiles) { using (Bitmap img = new Bitmap(filePath)) { images.Add(new Bitmap(img)); // 复制一份避免原文件被锁定 totalWidth += img.Width; if (img.Height > maxHeight) maxHeight = img.Height; } } // 创建透明背景的目标Bitmap using (Bitmap combinedBitmap = new Bitmap(totalWidth, maxHeight, PixelFormat.Format32bppArgb)) { using (Graphics g = Graphics.FromImage(combinedBitmap)) { // 设置绘图模式,确保透明图片正确叠加 g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver; g.Clear(Color.Transparent); // 清空为透明背景 int currentX = 0; foreach (Bitmap img in images) { // 图片垂直居中绘制,也可改为顶部对齐:currentY = 0 int currentY = (maxHeight - img.Height) / 2; g.DrawImage(img, currentX, currentY); currentX += img.Width; img.Dispose(); // 释放临时图片资源 } // 保存最终图片,确保保留透明通道 combinedBitmap.Save(outputPath, ImageFormat.Png); } } } }
情况2:按网格排列图片
如果需要将图片按固定列数排列成网格,可使用以下代码:
using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Collections.Generic; class GridImageCombiner { static void Main(string[] args) { string imageFolder = @"C:\YourImageFolderPath"; string outputPath = @"C:\CombinedGridImage.png"; int columns = 3; // 自定义列数 string[] pngFiles = Directory.GetFiles(imageFolder, "*.png", SearchOption.TopDirectoryOnly); if (pngFiles.Length == 0) return; // 预加载图片并计算网格尺寸(假设所有图片尺寸一致,若不同可改为取最大宽高) List<Bitmap> images = new List<Bitmap>(); int imgWidth = 0; int imgHeight = 0; using (Bitmap firstImg = new Bitmap(pngFiles[0])) { imgWidth = firstImg.Width; imgHeight = firstImg.Height; } foreach (string filePath in pngFiles) { images.Add(new Bitmap(filePath)); } int rows = (int)Math.Ceiling((double)images.Count / columns); int totalWidth = columns * imgWidth; int totalHeight = rows * imgHeight; using (Bitmap combinedBitmap = new Bitmap(totalWidth, totalHeight, PixelFormat.Format32bppArgb)) { using (Graphics g = Graphics.FromImage(combinedBitmap)) { g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver; g.Clear(Color.Transparent); for (int i = 0; i < images.Count; i++) { int row = i / columns; int col = i % columns; int x = col * imgWidth; int y = row * imgHeight; g.DrawImage(images[i], x, y); images[i].Dispose(); } combinedBitmap.Save(outputPath, ImageFormat.Png); } } } }
关键注意事项
- 必须使用
PixelFormat.Format32bppArgb创建Bitmap,这是支持透明通道的像素格式 - 绘制时设置
CompositingMode.SourceOver,确保PNG的透明区域不会覆盖背景 - 图片加载后要及时释放资源,避免内存泄漏,推荐使用
using语句 - 保存时选择
ImageFormat.Png,JPEG格式不支持透明通道
内容的提问来源于stack exchange,提问作者Keras-JOB
相关产品推荐
相关产品推荐

