How to draw a Number to an Image Delphi 7
Asked Answered
T

1

3

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.

Thimbu answered 22/9, 2011 at 4:51 Comment(0)
O
8

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;
Outgeneral answered 22/9, 2011 at 5:20 Comment(7)
I would have GenerateImageFromNumber() return a TBitmap that could be assigned to the TImage, or have it draw to the TImage directly, without using a temporary file at all.Subdued
@rakesh, If this solves your problem, then you should accept RRUZ's answer.Keever
@Remy, sure the procedure can be modified to return a bitmap instead of store the image in a file. btw I wrote the sample thinking in store the generated image, and not using the file as a temporally buffer :)Outgeneral
Is it possible to draw a number on icon as like above. ?Thimbu
Yes, but is more complicated, because The icon format (handled by the TIcon class) is totally different and has many limitations like the canvas and the final size of the image. Maybe the best option is draw the bmp and then convert to a icon.Outgeneral
@RRUZ: FYI, I used your code hereTraherne
@rakesh: I've already developped a small application with a countdown which displays the current number on the application's button of the taskbar with Windows 7... Check this application if you have Windows 7, and if it's what you want, ask a new question which explains that you wanna this, and I will paste the relevant code...Traherne

© 2022 - 2024 — McMap. All rights reserved.