Recent

Author Topic: Tile image as background  (Read 24674 times)

BenJones

  • Full Member
  • ***
  • Posts: 121
Tile image as background
« on: May 17, 2011, 01:12:32 pm »
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
When Your Dreams Come True.

BenJones

  • Full Member
  • ***
  • Posts: 121
Re: Tile image as background
« Reply #1 on: May 17, 2011, 01:59:47 pm »
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.
When Your Dreams Come True.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Tile image as background
« Reply #2 on: May 17, 2011, 02:19:22 pm »
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?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

BenJones

  • Full Member
  • ***
  • Posts: 121
Re: Tile image as background
« Reply #3 on: May 17, 2011, 02:32:26 pm »
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.  :)
When Your Dreams Come True.

BenJones

  • Full Member
  • ***
  • Posts: 121
Re: Tile image as background
« Reply #4 on: May 17, 2011, 03:12:06 pm »
Ok i found out what I was missing i was missing windows from the uses.
When Your Dreams Come True.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Tile image as background
« Reply #5 on: May 17, 2011, 03:20:10 pm »
You don't need to create a TBrush, it's already there, ready to be used.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Tile image as background
« Reply #6 on: May 17, 2011, 05:27:39 pm »
Quote
Ok 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

  • Full Member
  • ***
  • Posts: 121
Re: Tile image as background
« Reply #7 on: May 17, 2011, 06:57:51 pm »
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
When Your Dreams Come True.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Tile image as background
« Reply #8 on: May 17, 2011, 07:39:17 pm »
Did you removed both "b:=TBrush.Create" and "b.Free;" ?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

BenJones

  • Full Member
  • ***
  • Posts: 121
Re: Tile image as background
« Reply #9 on: May 17, 2011, 11:03:38 pm »
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;
« Last Edit: May 17, 2011, 11:06:20 pm by BenJones »
When Your Dreams Come True.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Tile image as background
« Reply #10 on: May 17, 2011, 11:12:30 pm »
You 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.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

BenJones

  • Full Member
  • ***
  • Posts: 121
Re: Tile image as background
« Reply #11 on: May 17, 2011, 11:26:18 pm »
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.
When Your Dreams Come True.

davesimplewear

  • Sr. Member
  • ****
  • Posts: 319
    • Davids Freeware
Re: Tile image as background
« Reply #12 on: May 18, 2011, 05:22:32 am »
I don't believe in reinventing the wheel, JanTilePanel does the job.

Regards
Dave
All things considered insanity seems the best option

Gintas

  • Jr. Member
  • **
  • Posts: 71
    • Developer's Diary
Re: Tile image as background
« Reply #13 on: September 28, 2011, 09:45:00 pm »
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

  • Guest
Re: Tile image as background
« Reply #14 on: September 29, 2011, 12:43:44 am »
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.

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);   
« Last Edit: September 29, 2011, 12:57:17 am by lainz »

 

TinyPortal © 2005-2018