Delphi:如何在图像中添加文本并保存新图像? [英] Delphi: how Add text to an image and save the new image?

查看:431
本文介绍了Delphi:如何在图像中添加文本并保存新图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个想法是从数据库中检索日期(文本和图片),然后将这些数据添加到另一张图片(如ID表格)中,然后保存新图片。

the idea is retrieve date from a database (text and a picture) then add these data in another picture (like an ID form) then save the new picture.

如何在delphi中完成?

how can this be done in delphi?

thx

推荐答案

请尝试以下方法:

uses
  PNGImage;

procedure TForm1.Button1Click(Sender: TObject);
var
  PNGImage: TPNGImage;
  BlobStream: TMemoryStream;
begin
  // create the PNG image instance
  PNGImage := TPNGImage.Create;
  try
    // assuming you have in the BlobStream variable the image from a DB loaded
    PNGImage.LoadFromStream(BlobStream);
    // setup the text background to be transparent
    PNGImage.Canvas.Brush.Style := bsClear;
    // optionally configure the font
    PNGImage.Canvas.Font.Size := 11;
    PNGImage.Canvas.Font.Color := clRed;
    PNGImage.Canvas.Font.Style := [fsBold];
    // and render it to the image's canvas
    PNGImage.Canvas.TextOut(5, 5, 'SomeText');
    // save this modified image to the file
    PNGImage.SaveToFile('c:\picture.png');
  finally
    // and finally free the PNG image instance
    PNGImage.Free;
  end;
end;

这是一个例子,我将如何创建我的访问卡(不要忘记保存 必要图片 文件 d:\ pimas.png ):

Here is an example how would I create my visit card (don't forget to save the necessary image file as d:\llamas.png):

uses
  GraphUtil, PNGImage;

procedure CreateCard(const AFileFile: string; AImage: TPNGImage;
  const AName, ASurname: string);
begin
  with TPNGImage.CreateBlank(COLOR_RGB, 8, 330, 160) do
  try
    GradientFillCanvas(Canvas, clWhite, $000080FF,
      Canvas.ClipRect, gdVertical);
    Canvas.StretchDraw(Rect(18, 18, 108, 108), AImage);
    Canvas.Pen.Width := 2;
    Canvas.Brush.Style := bsClear;
    Canvas.Rectangle(5, 5, Width - 4, Height - 4);
    Canvas.Font.Size := 12;
    Canvas.Font.Style := [fsBold];
    Canvas.TextOut(110, 30, 'Form:  ' + AName + '  :.');
    Canvas.TextOut(125, 60, 'Genus:  ' + ASurname + '  :.');
    SaveToFile(AFileFile);
  finally
    Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  PNGImage: TPNGImage;
begin
  PNGImage := TPNGImage.Create;
  try
    // here you will load the image blob (by using LoadFromStream)
    // instead of LoadFromFile
    PNGImage.LoadFromFile('d:\llamas.png');
    CreateCard('d:\visit-card.png', PNGImage, 'Alpaca', 'Lama');
  finally
    PNGImage.Free;
  end;
end;

以下是它的样子:

这篇关于Delphi:如何在图像中添加文本并保存新图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆