两张BMP图片叠加

procedure BlendImage(BackBmp, ForeBmp: TBitmap; TransColor: TColor; Alpha: Integer);

type
  TRGBArray = array[0..32767] of TRGBTriple;
  PRGBArray = ^TRGBArray;

var
  X, Y, OffX, OffY: Integer;
  BackRect, ForeRect: TRect;
  BackRGB, ForeRGB: PRGBArray;
  BackR, BackG, BackB: Byte;
  ForeR, ForeG, ForeB: Byte;
begin
  if not Assigned(BackBmp) then exit;
  if not Assigned(ForeBmp) then exit;
  if BackBmp.PixelFormat <> pf24bit then Exit;
  if ForeBmp.PixelFormat <> pf24bit then Exit;
  if (BackBmp.Empty) or (ForeBmp.Empty) then Exit;
  BackRect := BackBmp.Canvas.ClipRect;
  ForeRect := ForeBmp.Canvas.ClipRect;
  DockRect(ForeRect, BackRect);
  OffX := ForeRect.Left - BackRect.Left;
  OffY := ForeRect.Top - BackRect.Top;
  for Y := 0 to BackBmp.Height - 1 do
  begin
    if (Y > ForeRect.Bottom - 1) or
      (Y < ForeRect.Top) then Continue;
    BackRGB := BackBmp.ScanLine[Y];
    ForeRGB := ForeBmp.ScanLine[Y - OffY];
    for X := 0 to BackBmp.Width - 1 do
    begin
      if (X > ForeRect.Right - 1) or
        (X < ForeRect.Left) then Continue;
      BackR := BackRGB[X].rgbtRed;
      BackG := BackRGB[X].rgbtGreen;
      BackB := BackRGB[X].rgbtBlue;
      ForeR := ForeRGB[X - OffX].rgbtRed;
      ForeG := ForeRGB[X - OffX].rgbtGreen;
      ForeB := ForeRGB[X - OffX].rgbtBlue;
      if (ForeR = GetRValue(TransColor)) and
        (ForeG = GetGValue(TransColor)) and
        (ForeB = GetBValue(TransColor)) then Continue;
      if (Alpha < 0) or (Alpha > 255) then Alpha := 255;
      BackRGB[X].rgbtRed := (BackR * (255 - Alpha) + ForeR * Alpha) div 255;
      BackRGB[X].rgbtGreen := (BackG * (255 - Alpha) + ForeG * Alpha) div 255;
      BackRGB[X].rgbtBlue := (BackB * (255 - Alpha) + ForeB * Alpha) div 255;
    end;
  end;
end;

猜你喜欢

转载自blog.csdn.net/qq_43466604/article/details/85610560