delphi 在指定图片上绘制透明文字

2018-10-30

function TForm1.DrawPicToStrSave(strFarImg1, strFont1,
strSavePath: string): TBitMap;
var
jp1, jp: TJPEGImage;
bmp_t: TBitmap;
begin
try
jp1 := TJPEGImage.Create;
jp := TJPEGImage.Create;
bmp_t := TBitmap.Create;
try
jp1.LoadFromFile(strFarImg1);
try
if (jp1.Width > 0) or (jp1.Height >0) then begin
bmp_t.Assign(jp1);
bmp_t.Transparent := True;
bmp_t.Canvas.Font.Color :=clRed; // 新建个水印字体颜色
bmp_t.Canvas.Pen.Style:=psClear;
bmp_t.Canvas.Brush.Style := bsClear;
bmp_t.Canvas.Font.Size := 18;
bmp_t.Canvas.Font.Name := 'System';
bmp_t.Canvas.TextOut(0,5,strFont1); // 加入文字
jp.Assign(bmp_t);
jp.SaveToFile(strSavePath);
Result := bmp_t;
end;
bmp_t.Assign(jp);
Image1.Picture.Bitmap.Assign(bmp_t);
finally
if assigned(bmp_t) then
bmp_t.Free;
end;
finally
jp1.Free;
jp.Free;
end;

except
Result := nil;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
DrawPicToStrSave('D:\aa.jpg','delphi网','D:\a1.jpg');
end;

阅读146