2012-05-01 63 views
2

如何将两个PNG合并在一起?我知道你不能使用PNGObject.Draw,因为它不会复制alpha转换(我不确定,但它不起作用),所以需要自定义过程/函数。我不是来跟两手空空,我有这样的过程:在其他PNG上绘制PNG图像

procedure MergePNGLayer(Layer1,Layer2: TPNGObject; Const aLeft,aTop:Integer); 
var 
    x, y: Integer; 
    SL1, SL2, SLBlended : pRGBLine; 
    aSL1, aSL2, aSLBlended: PByteArray; 
    blendCoeff: single; 
    blendedPNG, Lay2buff: TPNGObject; 
begin 
    blendedPNG:=TPNGObject.Create; 
    blendedPNG.Assign(Layer1); 
    Lay2buff:=TPNGObject.Create; 
    Lay2buff.Assign(Layer2); 
    SetPNGCanvasSize(Layer2,Layer1.Width,Layer1.Height,aLeft,aTop); 
    for y := 0 to Layer1.Height-1 do 
    begin 
    SL1 := Layer1.Scanline[y]; 
    SL2 := Layer2.Scanline[y]; 
    aSL1 := Layer1.AlphaScanline[y]; 
    aSL2 := Layer2.AlphaScanline[y]; 
    SLBlended := blendedPNG.Scanline[y]; 
    aSLBlended := blendedPNG.AlphaScanline[y]; 
    for x := 0 to Layer1.Width-1 do 
    begin 
     blendCoeff:=aSL1[x] * 100/255/100; 
     aSLBlended[x] := round(aSL2[x] + (aSL1[x]-aSL2[x]) * blendCoeff); 
     SLBlended[x].rgbtRed := round(SL2[x].rgbtRed + (SL1[x].rgbtRed-SL2[x].rgbtRed) * blendCoeff); 
     SLBlended[x].rgbtGreen := round(SL2[x].rgbtGreen + (SL1[x].rgbtGreen-SL2[x].rgbtGreen) * blendCoeff); 
     SLBlended[x].rgbtBlue := round(SL2[x].rgbtBlue + (SL1[x].rgbtBlue-SL2[x].rgbtBlue) * blendCoeff); 
    end; 
    end; 
Layer1.Assign(blendedPNG); 
Layer2.Assign(Lay2buff); 
blendedPNG.Free; 
Lay2buff.Free; 
end; 

但可悲的是它不工作应该如何,它的工作,但不正确的。当它将空图像与加载的图像合并在一起时,它可以很好地工作,但是当两幅图像都不是空的时候,这会使它们失去透明度。

procedure TForm1.FormClick(Sender: TObject); 
var 
PNG1, PNG2, PNG3, Dest: TPNGObject; 
begin 
PNG1 := TPNGObject.Create; 
PNG2 := TPNGObject.Create; 
PNG3 := TPNGObject.Create; 

PNG1.LoadFromFile('Aero\TopLeft.png');//Width 10 
PNG2.LoadFromFile('Aero\Top.png');//Width 200 
PNG3.LoadFromFile('Aero\TopRight.png');//Width 10 

Dest := TPNGObject.CreateBlank(COLOR_RGBALPHA, 16, 220, 10); 
MergePNGLayer(Dest, PNG1, 0, 0); 
MergePNGLayer(Dest, PNG2, 10, 0); 
MergePNGLayer(Dest, PNG3, 210, 0); 
Dest.SaveToFile('C:\OUT.PNG'); 
end; 

通缉的结果:

enter image description here

实际结果:

enter image description here

我不知道,如果你能看到这些imgaes之间的差异,但你应该在打开这些PNG编辑器软件,你会看到不同之处。所以我需要其他程序来合并PNG。顺便提一句,我正在使用最新版本的PNGImage

谢谢,祝你有美好的一天!

+1

您确定要'CreateBlank'与'一位深度= 16' 4个颜色通道? – TLama

回答

5

这似乎就好了工作:

procedure DrawPngWithAlpha(Src, Dest: TPNGObject; const R: TRect); 
var 
    X, Y: Integer; 
    Alpha: PByte; 
begin 
    Src.Draw(Dest.Canvas, R); 

    // I have no idea why standard implementation of TPNGObject.Draw doesn't apply transparency. 
    for Y := R.Top to R.Bottom - 1 do 
    for X := R.Left to R.Right - 1 do 
    begin 
     Alpha := @Dest.AlphaScanline[Y]^[X]; 
     Alpha^ := Min(255, Alpha^ + Src.AlphaScanline[Y - R.Top]^[X - R.Left]); 
    end; 
end; 
+1

在你试图调用'Draw'之前什么是'TPngObject.TransparencyMode'?那是不是'ptmBit = 1'? +1无论如何为简单*可能*解决方法;-) – TLama

+0

@TLama以前没有使用任何'TransparencyMode',所有图像都是_fresh_ –