合并两个具有透明度的png图像并保留透明度 [英] Merge two png images with transparency and retain transparency

查看:135
本文介绍了合并两个具有透明度的png图像并保留透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在C#/.NET中合并两个图像

Possible Duplicate:
Merging two images in C#/.NET

我有两个png格式的图像,并且都定义了透明度.我需要将这些合并到一个新的png图像中,但又不损失结果的任何透明度.将第一个图像视为主图像,将第二个图像用于添加叠加层,例如添加/编辑/删除指示符.我正在尝试创建一个小的实用程序,该实用程序将获取一个主图像和一组叠加层,然后生成将它们组合在一起的结果输出图像集.

I have two png format images and both have transparency defined. I need to merge these together into a new png image but without losing any of the transparency in the result. Think of the first image as the main image and the second is used to add an overlay, such as a add/edit/delete indicator. I am trying to create a little utility that will take a main image and a set of overlays and then generate the resultant set of output images that combine them.

PHP解决方案似乎有很多答案,但C#/

There seem to be plenty of answers with solutions for PHP but nothing for C#/

推荐答案

这应该有效.

Bitmap source1; // your source images - assuming they're the same size
Bitmap source2;
var target = new Bitmap(source1.Width, source1.Height, PixelFormat.Format32bppArgb);
var graphics = Graphics.FromImage(target);
graphics.CompositingMode = CompositingMode.SourceOver; // this is the default, but just to be clear

graphics.DrawImage(source1, 0, 0);
graphics.DrawImage(source2, 0, 0);

target.Save("filename.png", ImageFormat.Png);

这篇关于合并两个具有透明度的png图像并保留透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆