2011-09-22 65 views

回答

7

您可以使用TBitmapCanvas屬性來繪製文本圖像中

檢查此過程

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( )返回一個可以分配給TImage的TBitmap,或者直接將其繪製到TImage,而不使用臨時文件。 –

+0

謝謝大家......! – rakesh

+0

@rakesh,如果這解決了你的問題,那麼你應該接受RRUZ的答案。 –