unit untImagePaintText;
interface
uses jpeg, Classes, Graphics, SysUtils;
///
/// 打开一幅图片(jpg,bmp)在上写入文字
///
/// 写入是否成功
function ImagePaintText(FilePath: string; X, Y: Integer; Texts: TStringList): Boolean;
implementation
function JpgToBmp(FilePath: string): string;
var
MyJPEG: TJPEGImage;
MyBMP: TBitmap;
s: string;
begin
Result := '';
s := copy(FilePath, 1, Length(FilePath) - 4) + FormatDateTime('YYYYMMDDhhmmsszzz', Now) + '.bmp';
MyJPEG := TJPEGImage.Create;
with MyJPEG do
begin
LoadFromFile(FilePath);
MyBMP := TBitmap.Create;
with MyBMP do
begin
Width := MyJPEG.Width;
Height := MyJPEG.Height;
Canvas.Draw(0, 0, MyJPEG);
SaveToFile(s);
Result := s;
Free;
end;
Free;
end;
end;
function BmpToJpg(FilePath, FilePathJpg: string): string;
var
Jpg: TJpegImage;
BMP: TBitMap;
s: string;
begin
Jpg := TJpegImage.Create;
BMP := TBitmap.Create;
BMP.LoadFromFile(FilePath);
Jpg.Assign(BMP);
Jpg.SaveToFile(FilePathJpg);
BMP.Free;
Jpg.Free;
end;
function ImagePaintText(FilePath: string; X, Y: Integer; Texts: TStringList): Boolean;
var
Pic: TPicture;
fp: string;
i: Integer;
begin
Result := false;
if UpperCase(ExtractFileExt(FilePath)) = UpperCase('.jpg') then
fp := JpgToBmp(FilePath)
else
fp := FilePath;
pic := TPicture.Create;
try
Pic.LoadFromFile(fp);
with Pic.Bitmap.Canvas do
begin
Brush.Style := bsClear;
Font.Size := 15;
Font.Color := clWhite;
Font.Name := '黑体';
for i := 0 to Texts.Count - 1 do
TextOut(x, y + (i * 22), Texts[i])
end;
if UpperCase(ExtractFileExt(FilePath)) = UpperCase('.jpg') then
begin
Pic.SaveToFile(fp);
BmpToJpg(fp, FilePath);
DeleteFile(fp);
end
else
Pic.SaveToFile(FilePath);
finally
FreeAndNil(Pic);
end;
Result := True;
end;
end.
//首引用单元untImagePaintText
//调用
//procedure TForm1.btn1Click(Sender: TObject);
//var
// sl: TStringList;
//begin
// if dlgOpen1.Execute then
// begin
// img1.Picture.LoadFromFile(dlgOpen1.FileName);
// sl := TStringList.Create;
// sl.Add('你好,彭XX');
// sl.Add('你好,习XX');
// if ImagePaintText(dlgOpen1.FileName, 20, 30, sl) then
// img1.Picture.LoadFromFile(dlgOpen1.FileName);
// end;
//end;
————————————————
原文链接:https://blog.csdn.net/lqena/article/details/22858847