将两个png图像合并为透明度并保持透明度

时间:2011-07-27 10:18:41

标签: c# png transparency

  

可能重复:
  Merging two images in C#/.NET

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

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

没有

2 个答案:

答案 0 :(得分:19)

这应该有用。

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);

答案 1 :(得分:1)

很遗憾你还没有提到如何获得像素,

所以p代码:

// The result will have its alpha chanell from "first", 
// the color channells from "second".

assert (first.width = second.width)
assert (first.height = second.height)

for y in 0..height
    for x in 0..width
        RGBA col_first  = first(x,y)
        RGBA col_second = second(x,y)

        result(x,y) =  RGBA(col_second.r,
                            col_second.g,
                            col_second.b,
                            col_first.a  ))
相关问题