比方说:我现在有一张图片A是JPG格式的图片,还有一张图片B是GIF格式的图片(注:B图片背景是透明的),我现在可以把A和B图片合并在一起生成一个新的JPG图片C,可以生成后的C图片,原来GIF图片的位置背景本来是透明的,现在变成白的了。怎么办啊?
我想要达到的图片效果是:http://www.yeagen.com/other/yes.jpg
而现在我只能达到的效果是:http://www.yeagen.com/other/no.jpg  以下是我合并时用的代码:
procedure TForm1.Button1Click(Sender: TObject);
var
   JPEGImage1:  TJPEGImage;
   JPEGImage2: TGIFImage;
   tmpImage1, tmpImage2 :TImage;
   ARect :TRect;
   AWidth, AHeight :integer;
begin
    DoubleBuffered:=true;
    tmpImage1 := TImage.Create(Self);
    tmpImage2 := TImage.Create(Self);
    tmpImage1.AutoSize:=true;
    tmpImage2.AutoSize:=true;//先读入jpeg格式的图片
    JPEGImage1 := TJPEGImage.Create;
    JPEGImage1.LoadFromFile('d:\a.jpg');
    tmpImage1.Picture.Bitmap.Assign(JPEGImage1);
    JPEGImage2 := TGifImage.Create;
    JPEGImage2.LoadFromFile('d:\bb.gif');
    tmpImage2.Picture.Bitmap.Assign(JPEGImage2);
    AWidth := tmpImage1.Width;
    AHeight:= tmpImage1.Height;
    tmpImage1.AutoSize:=false;  bitblt(tmpimage1.Canvas.Handle,0,0,screen.Width,screen.Height,tmpimage2.Canvas.handle,0,0,srccopy);
  JPEGImage1.Assign(tmpImage1.Picture.Bitmap);
  JPEGImage1.Compress;
  JPEGImage1.SaveToFile('d:\c.jpg');
  tmpimage1.Free;
  tmpimage2.Free;
  JPEGImage1.Free;
  JPEGImage2.Free;
end;

解决方案 »

  1.   

    你如果想将一张具有不规则图形的图片以半透明方式放在另一张图片上,可以用以下的程序:
    type
      tagRGBTRIPLE = packed record
        rgbtBlue: Byte;
        rgbtGreen: Byte;
        rgbtRed: Byte;
      end;
      TRGBTriple = tagRGBTRIPLE;
      PRGBTripleArray = ^TRGBTripleArray;
      TRGBTripleArray = array[0..32767] of TRGBTriple;
     procedure BmpAlphaBlend(var dBmp: TBitMap; sBmp: TBitmap; Pos: TPoint; Alpha: integer; TranColor: TColor = -1);
    function GetSLColor(pRGB: TRGBTriple): TColor;
    begin
      Result := RGB(pRGB.rgbtRed, pRGB.rgbtGreen, pRGB.rgbtBlue);
    end;var
      p0, p1: PRGBTripleArray;
      r, g, b, p, x, y: Integer;
    begin
      sBmp.PixelFormat := pf24bit;
      dBmp.PixelFormat := pf24bit;
      if TranColor = -1 then
        TranColor := sBmp.Canvas.Pixels[0, 0];
      for y := 0 to sBmp.Height - 1 do
        if (y + Pos.y >= 0) and (y + Pos.Y < dBmp.Height) then
          begin
            p0 := dBmp.ScanLine[y + Pos.y];
            p1 := sBmp.ScanLine[y];
            for x := 0 to sBmp.Width - 1 do
              if (x + pos.X >= 0) and (x + Pos.X < dBmp.Width) then
                if GetSLCOlor(p1[x]) <> TranColor then
                  begin
                    p0[x + pos.X].rgbtRed := IntToByte((p0[x + pos.X].rgbtRed * (100 - Alpha) +
                      p1[x].rgbtRed * Alpha) div 100);                p0[x + pos.X].rgbtGreen := IntToByte((p0[x + pos.X].rgbtGreen * (100 - Alpha) +
                      p1[x].rgbtGreen * Alpha) div 100);                p0[x + pos.X].rgbtBlue := IntToByte((p0[x + pos.X].rgbtBlue * (100 - Alpha) +
                      p1[x].rgbtBlue * Alpha) div 100);
                  end;
          end;end;
      

  2.   

    谢谢: xzhifei(星级饭桶(抵制日货)·飞) 用你的方法已经解决了我的问题