Mega Code Archive

 
Categories / Delphi / Graphic
 

Converting text to gif images

Title: Converting text to gif images Question: Sometimes, it is necessary to convert text to Gif images, to put in web pages. This article shows how to convert text to Gif images, developing a component to do it Answer: To convert some text to an image of any kind, the only thing to do is to create this image and draw the text on its canvas. This code converts a text in a memo to a 100 x 100 bitmap: var Bitmap : TBitmap; begin Bitmap := TBitmap.Create; try Bitmap.Width := 100; Bitmap.Height := 100; Bitmap.Canvas.TextOut(0,0,Memo1.Text); Bitmap.SaveToFile('text.bmp'); finally Bitmap.Free; end; end; This code doesn't handle wordwraps. The easiest way to handle text wordwrap in custom widths is using Windows API DrawText function. Its last parameter is a Flags parameter, where you can specify DT_WORDBREAK to handle word breaks. You must supply a rectangle and it will break the text to fit. These lines show how to do it: R := Rect(0,0,Bitmap.Width,Bitmap.Height); DrawText(Handle,PChar(Memo1.Text),-1,R,DT_NOPREFIX or DT_WORDBREAK); Delphi doesn't support Gif images by default. To convert the text to a Gif image, you must download Anders Melander's TGifImage component (http://www.melander.dk/delphi/gifimage). Then, you can use TGifImage to create a Gif image from the text: var Gif : TGifImage; R : TRect; Bitmap : TBitmap; begin // creates a bitmap to draw the text Bitmap := TBitmap.Create; try Bitmap.Width := 100; Bitmap.Height := 100; Gif := TGifImage.Create; try Gif.Width := 100; Gif.Height := 100; // draw the text in the bitmap canvas with Bitmap.Canvas do begin R := Rect(0,0,Bitmap.Width,Bitmap.Height); DrawText(Handle,PChar(Memo1.Text),-1,R,DT_NOPREFIX or DT_WORDBREAK); end; // assign the bitmap to the gif image Gif.Assign(Bitmap); // create the gif file Gif.SaveToFile('test.gif'); finally Gif.Free; end; finally Bitmap.Free; end; With this information, we can now create a new component, TText2Gif, that converts some text to a Gif image. This component has three properties, Width, Height and Font, to select the image Width and Height and the font of the drawn text. It has one method, CreateGifFile(aText, aFullPath: string), which takes a text and creates a Gif image. The full code of the component is this: unit Txt2Gif; interface uses Windows, SysUtils, Classes, GifImage, Graphics, Types; type TTxt2Gif = class(TComponent) private FHeight: Integer; FWidth: Integer; FFont: TFont; procedure SetFont(const Value: TFont); procedure SetHeight(const Value: Integer); procedure SetWidth(const Value: Integer); { Private declarations } protected { Protected declarations } public { Public declarations } constructor Create(AOwner : TComponent); override; destructor Destroy; override; procedure CreateGifFile(aText, aFullPath: string); published { Published declarations } property Width : Integer read FWidth write SetWidth default 100; property Height : Integer read FHeight write SetHeight default 100; property Font : TFont read FFont write SetFont; end; procedure Register; implementation procedure Register; begin RegisterComponents('BSComps', [TTxt2Gif]); end; { TTxt2Gif } constructor TTxt2Gif.Create(AOwner: TComponent); begin inherited; FWidth := 100; FHeight := 100; FFont := TFont.Create; end; procedure TTxt2Gif.CreateGifFile(aText, aFullPath: string); var Gif : TGifImage; R : TRect; Bitmap : TBitmap; begin // create the bitmap to draw the text Bitmap := TBitmap.Create; try Bitmap.Width := FWidth; Bitmap.Height := FHeight; Gif := TGifImage.Create; try Gif.Width := FWidth; Gif.Height := FHeight; // draw the text with Bitmap.Canvas do begin Font.Assign(FFont); R := Rect(0,0,FWidth,FHeight); DrawText(Handle,PChar(aText),-1,R,DT_NOPREFIX or DT_WORDBREAK); end; // assign the bitmap to the gif image Gif.Assign(Bitmap); // save the image to a file Gif.SaveToFile(aFullPath); finally Gif.Free; end; finally Bitmap.Free; end; end; destructor TTxt2Gif.Destroy; begin FFont.Free; inherited; end; procedure TTxt2Gif.SetFont(const Value: TFont); begin FFont := Value; end; procedure TTxt2Gif.SetHeight(const Value: Integer); begin FHeight := Value; end; procedure TTxt2Gif.SetWidth(const Value: Integer); begin FWidth := Value; end; end. To use this component, you only have to install it in the palette, drop one in our Form. set the width, height and font and use some code like this: Txt2Gif1.CreateGifFile(Memo1.Text,'test.gif');