Search code examples
delphidelphi-7

How to draw a Number to an Image Delphi 7


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.


Solution

  • 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;