unit UAlphaBlend;

interface

uses
  Windows, Graphics;

procedure AlphaImgs(Image1, Image2: TGraphic; ResultBitmap: TBitmap; AlphaValue: Byte);

implementation

uses SysUtils;

procedure AlphaImgs(Image1, Image2: TGraphic; ResultBitmap: TBitmap; AlphaValue: Byte);
  var
    TempBitmap: TBitmap;
    x, y, h, w: Integer;
    Pixel1, Pixel2: PRGBTriple;
begin
  if Assigned(Image1) and Assigned(Image2) and Assigned(ResultBitmap) then begin
    TempBitmap := TBitmap.Create;
    try
      ResultBitmap.Assign(Image1);
      ResultBitmap.PixelFormat := pf24bit;
      TempBitmap.Height := Image2.Height;
      TempBitmap.Width := Image2.Width;
      TempBitmap.PixelFormat := pf24bit;
      TempBitmap.Canvas.Draw(0, 0, Image2);
      if (ResultBitmap.Width < TempBitmap.Width) then
        w := ResultBitmap.Width
      else
        w := TempBitmap.Width;
      if (ResultBitmap.Height < TempBitmap.Height) then
        h := ResultBitmap.Height
      else
        h := TempBitmap.Height;
      for y := 0 to h-1 do begin
        Pixel1 := ResultBitmap.ScanLine[y];
        Pixel2 := TempBitmap.ScanLine[y];
        for x := 0 to w-1 do begin
          Pixel1^.rgbtBlue  := (Pixel1^.rgbtBlue  *AlphaValue +(Pixel2^.rgbtBlue  *(255 -AlphaValue))) shr 8;
          Pixel1^.rgbtGreen := (Pixel1^.rgbtGreen *AlphaValue +(Pixel2^.rgbtGreen *(255 -AlphaValue))) shr 8;
          Pixel1^.rgbtRed   := (Pixel1^.rgbtRed   *AlphaValue +(Pixel2^.rgbtRed   *(255 -AlphaValue))) shr 8;
          Inc(Pixel1);
          Inc(Pixel2);
        end;
      end;
    finally
      TempBitmap.Free;
    end;
  end
  else
    raise Exception.Create('Parameterfehler!');
end;

end.
