How to fill a text with a bitmap (Views: 27)
Problem/Question/Abstract: How to fill a text with a pattern or bitmap? I know that using fillpath(dc) will do it but how can I assign the bitmap or a gradient color to the font? Answer: Solve 1: Here's one method. To make this work, add a TImage and load it with a bmp of about 256 x 256. Make the TImage Visible - False. Drop a TButton on the form. Hook up the OnClick for the button. Change the Form's Font to be something big enough, say around 80 for the size. Also, specify a True Type Font (I used Times New Roman). unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Image1: TImage; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); var ClipRegion: HRGN; Bmp: TBitmap; begin Bmp := TBitmap.Create; Bmp.Width := Image1.Width; Bmp.Height := Image1.Height; Bmp.Canvas.Brush.Color := clSilver; {You could use a different color, or another bitmap} Bmp.Canvas.FillRect(RECT(0, 0, Bmp.Width, Bmp.Height)); BeginPath(Canvas.Handle); SetBkMode(Canvas.Handle, TRANSPARENT); TextOut(Canvas.Handle, 10, 30, 'Cool!', 5); EndPath(Canvas.Handle); ClipRegion := PathToRegion(Canvas.Handle); SelectClipRgn(Bmp.Canvas.Handle, ClipRegion); Bmp.Canvas.Draw(0, 0, Image1.Picture.Bitmap); SelectClipRgn(Bmp.Canvas.Handle, 0); Canvas.Draw(0, 0, Bmp); DeleteObject(ClipRegion); Bmp.Free; end; end. Solve 2: procedure TForm1.Button1Click(Sender: TObject); var dc: hdc; SaveIndex: integer; bm: TBitmap; begin bm := TBitmap.Create; bm.LoadFromFile('c:\download\test.bmp'); Canvas.Font.Name := 'Arial'; Canvas.Font.Height := 100; dc := Canvas.Handle; SaveIndex := SaveDc(Dc); SetBkMode(dc, TRANSPARENT); BeginPath(dc); Canvas.TextOut(0, 0, 'Many TeamB guys ignore me'); EndPath(dc); SelectClipPath(dc, RGN_COPY); Canvas.Draw(0, 0, bm); RestoreDc(dc, SaveIndex); bm.Free; end; |