2012-05-01 60 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_ –