How to draw a Number to an Image Delphi 7

人走茶凉 提交于 2019-12-23 17:37:07

问题


I have a requirement to draw a number to a image.That number will changes automatically.how can we create an image dynamically in Delphi 7 ? .If any one knows please suggest me.

Yours Rakesh.


回答1:


You can use the Canvas property of a TBitmap to draw a text in a image

check this procedure

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;

And use like this

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


来源:https://stackoverflow.com/questions/7509718/how-to-draw-a-number-to-an-image-delphi-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!