Tile image as background lazarus / 39-Graphics ---------------------------------------------------------------------- BenJones · 2011-05-17 11:12 Hi i am new here, can someone point me in the right way to title a background image on a form, I been makeing a little CDMenu frontend and now I like to have the form background as a image. Thanks ---------------------------------------------------------------------- BenJones · 2011-05-17 11:59 Ok I am trying some code to sort the problum here is what I have Code: [Select]var    b : TBrush;    rc : TRect; begin   b := Tbrush.Create;   b.Bitmap := ImgBrush.Picture.Bitmap;   frmmain.Canvas.Brush := b;   SetRect(rc,0,0,self.Width ,self.Height);   frmmain.Canvas.FillRect(rc);   b.Free; But i get an error while running the code unit1.pas(218,10) Error: Identifier not found "SetRect" unit1.pas(219,29) Warning: Local variable "rc" does not seem to be initialized unit1.pas(255) Fatal: There were 1 errors compiling module, stopping It seems like I am missing something but not sure this is what I got in my uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,   ExtCtrls, inifiles,ShellApi,Unit2,MMSystem; Any ideas were I am going wrong thanks. ---------------------------------------------------------------------- Blaazen · 2011-05-17 12:19 For painting on canvas of form, you should move your code to OnPaint event of form, otherwise it does not work (at least here - Linux+Qt). Instead of SetRect use this: Code: [Select]var rc: TRect; begin   rc:=Rect(0, 0, self.Width, self.Height); You want tiles, i.e. fill background with one small image repeated vertically and horizontally? ---------------------------------------------------------------------- BenJones · 2011-05-17 12:32 Hi thanks that works perfect also moved my code to on paint . but I still like to know why setrect did not work did I have something wrong in the uses. Thanks agian. ---------------------------------------------------------------------- BenJones · 2011-05-17 13:12 Ok i found out what I was missing i was missing windows from the uses. ---------------------------------------------------------------------- garlar27 · 2011-05-17 13:20 You don't need to create a TBrush, it's already there, ready to be used. ---------------------------------------------------------------------- Leledumbo · 2011-05-17 15:27 QuoteOk i found out what I was missing i was missing windows from the uses.So don't use it if you want your app to stay cross platform. ---------------------------------------------------------------------- BenJones · 2011-05-17 16:57 I took the create brush out but it gave me an error Project project1.exe raised exception class 'External: SIGSEGV'. but when I put it back in it was ok ---------------------------------------------------------------------- Blaazen · 2011-05-17 17:39 Did you removed both "b:=TBrush.Create" and "b.Free;" ? ---------------------------------------------------------------------- BenJones · 2011-05-17 21:03 Hi yes removed borth still same errors here my code code is called form OnPaint Event Code: [Select]procedure TextureForm();   var    b : TBrush;    rc : TRect; begin   b.Bitmap := frmmain.ImgBrush.Picture.Bitmap;   //Set form brush   frmmain.Canvas.Brush := b;   //Set the size to draw, Thanks to Blaazen for helping with this Rect Thing   rc := Rect(0,0,frmmain.Width,frmmain.Height);   //Fill form with brush   frmmain.Canvas.FillRect(rc); end; ---------------------------------------------------------------------- Blaazen · 2011-05-17 21:12 Quote from: garlar27 on May 17, 2011, 03:20:10 pmYou don't need to create a TBrush, it's already there, ready to be used. garlar27 probably meant do it directly without "b", then you don't need to create and free it: Code: [Select]procedure TextureForm();   var  rc : TRect; begin   //Set form brush   frmmain.Canvas.Brush.Bitmap := frmmain.ImgBrush.Picture.Bitmap; Note I work with graphic only sometimes. ---------------------------------------------------------------------- BenJones · 2011-05-17 21:26 Thanks Blaazen that works fine now. Now I can finish thie project and start on my new project idea, thanks agian for the help. It realy helpfull. ---------------------------------------------------------------------- davesimplewear · 2011-05-18 03:22 I don't believe in reinventing the wheel, JanTilePanel does the job. Regards Dave ---------------------------------------------------------------------- Gintas · 2011-09-28 19:45 I wonder how to do the same with TBGRABitmap canvas. The example code in "testbgrafunc" which draw tile background doesn't make much sense to me. ---------------------------------------------------------------------- lainz · 2011-09-28 22:43 Quote from: Gintas on September 28, 2011, 09:45:00 pmI wonder how to do the same with TBGRABitmap canvas. The example code in "testbgrafunc" which draw tile background doesn't make much sense to me. First load the image. Then you need to use the GetPart procedure. And finally draw to the form. Code: [Select]procedure TForm1.FormCreate(Sender: TObject); begin   background := TBGRABitmap.Create('diamondback.png'); end; procedure TForm1.BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap); var   tile: TBGRABitmap; begin   tile := background.GetPart(rect(0,0,Width,Height)) as TBGRABitmap;   Bitmap.PutImage(0,0,tile,dmSet);   tile.Free; end; procedure TForm1.FormDestroy(Sender: TObject); begin   background.Free; end; In that case I was using a BGRAVirtualScreen, but you can draw OnPaint event of the form. Code: [Select]procedure TForm1.FormPaint(Sender: TObject); var   tile: TBGRABitmap; begin   tile := background.GetPart(rect(0,0,Width,Height)) as TBGRABitmap;   tile.Draw(Canvas,0,0);   tile.Free; end;    Edit: If you want to use the existing image just replace the creation of the background: Code: [Select] background := TBGRABitmap.Create(ImgBrush.Picture.Bitmap); ---------------------------------------------------------------------- lainz · 2011-09-28 23:17 BTW I've added a new property 'Tile' in BGRASpriteAnimation in the git repository of BGRAControls. ---------------------------------------------------------------------- circular · 2011-09-28 23:39 That's right Lainz, you can use GetPart. You can also use the Fill method. Instead of writing Code: [Select]var   tile: TBGRABitmap; begin   tile := background.GetPart(rect(0,0,Width,Height)) as TBGRABitmap;   Bitmap.PutImage(0,0,tile,dmSet);   tile.Free; end; You can write Code: [Select]begin   Bitmap.Fill(background); end; ---------------------------------------------------------------------- lainz · 2011-11-13 14:55 I can fill transparent bitmap? ---------------------------------------------------------------------- circular · 2011-11-13 16:02 Of course, with FillTransparent method. ---------------------------------------------------------------------- lainz · 2011-11-13 16:12 My question is if I can fill a bitmap with transparent area with Bitmap1.Fill(Bitmap2); (bitmap2 has alpha pixels); You can add procedure TBGRADefaultBitmap.Fill(texture: IBGRAScanner);   with a new option transparent: boolean, or drawmode: TDrawMode; ---------------------------------------------------------------------- circular · 2011-11-13 16:22 Ah ok, you mean when there are alpha pixels, they are blended and you would like to use dmSet mode instead ? ---------------------------------------------------------------------- lainz · 2011-11-13 18:08 Quote from: circular on November 13, 2011, 05:22:57 pmAh ok, you mean when there are alpha pixels, they are blended and you would like to use dmSet mode instead ? They aren't blended with Fill(bitmap) because by default is dmSet in: Code: [Select]procedure TBGRADefaultBitmap.Fill(texture: IBGRAScanner); begin   FillRect(FClipRect.Left,FClipRect.Top,FClipRect.Right,FClipRect.Bott om,texture,dmSet); end;  BTW I've solved the problem using FillRect. Code: [Select]battleroom.FillRect(0, 0, battleroom.Width, battleroom.Height, grid, dmDrawWithTransparency); ---------------------------------------------------------------------- circular · 2011-11-13 22:16 Ok. I've added Fill(IBGRAScanner, TDrawMode) on subversion ---------------------------------------------------------------------- lainz · 2011-11-14 00:53 Thx