如何在Delphi 7中将数字绘制到图像上

3
我有一个要求,需要在图片上绘制数字。这个数字将会自动改变。如何在Delphi 7中动态创建一个图片呢?如果有人知道,请给我建议。
您的,
Rakesh.
1个回答

8
你可以使用 Canvas 属性在图片中绘制文本,TBitmap 表示位图。

请查看此过程。

procedure GenerateImageFromNumber(ANumber:Integer;Const FileName:string);
Var
  Bmp : TBitmap;
begin
  Bmp:=TBitmap.Create;
  try
    Bmp.PixelFormat:=pf24bit;
    Bmp.Canvas.Font.Name :='Arial';// set the font to use
    Bmp.Canvas.Font.Size  :=20;//set the size of the font
    Bmp.Canvas.Font.Color:=clWhite;//set the color of the text
    Bmp.Width  :=Bmp.Canvas.TextWidth(IntToStr(ANumber));//calculate the width of the image
    Bmp.Height :=Bmp.Canvas.TextHeight(IntToStr(ANumber));//calculate the height of the image
    Bmp.Canvas.Brush.Color := clBlue;//set the background
    Bmp.Canvas.FillRect(Rect(0,0, Bmp.Width, Bmp.Height));//paint the background
    Bmp.Canvas.TextOut(0, 0, IntToStr(ANumber));//draw the number
    Bmp.SaveToFile(FileName);//save to a file
  finally
    Bmp.Free;
  end;
end;

并像这样使用

procedure TForm1.Button1Click(Sender: TObject);
begin
  GenerateImageFromNumber(10000,'Foo.bmp');
  Image1.Picture.LoadFromFile('Foo.Bmp');//Image1 is a TImage component
end;

4
我会将GenerateImageFromNumber()返回一个TBitmap,可以分配给TImage,或者直接在TImage上绘制,而不需要使用任何临时文件。 - Remy Lebeau
@rakesh,如果这个解决了你的问题,那么你应该接受RRUZ的答案。 - Bruce McGee
是的,但更加复杂,因为图标格式(由TIcon类处理)完全不同,并且有许多限制,例如画布和图像的最终大小。也许最好的选择是先绘制bmp,然后转换为图标。 - RRUZ
@RRUZ:顺便说一下,我在这里使用了你的代码(https://dev59.com/mFvUa4cB1Zd3GeqPuYZi#7517025) - Whiler
@rakesh:我已经开发了一个带有倒计时的小应用程序,它可以在Windows 7任务栏的按钮上显示当前数字。如果你使用Windows 7,可以检查一下这个应用程序,如果符合你的需求,请发布一个新问题并解释你需要的功能,我会粘贴相关代码... - Whiler
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接