Recent

Author Topic: Image background  (Read 257 times)

Roman

  • New Member
  • *
  • Posts: 21
Image background
« on: June 17, 2022, 12:42:29 pm »
I am opening a picture with
Code: Pascal  [Select][+][-]
  1. Form1.Image1.Picture.LoadFromFile(filename);

I wish to see a black background around the picture (there is some default color), but I am obviously too dumb to find how. Where should I look?

Handoko

  • Hero Member
  • *****
  • Posts: 4712
  • My goal: build my own game engine using Lazarus
Re: Image background
« Reply #1 on: June 17, 2022, 02:07:59 pm »
Just use something black larger than picture and put it behind the picture, maybe a TShape.

Below is an example using a TShape, all components are created runtime. Drop a button an a form and make sure you put StdCtrls, ExtDlgs, ExtCtrls in the uses clause.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const
  3.   ImageBorder = 5;
  4. const
  5.   Image:      TImage = nil;
  6.   Background: TShape = nil;
  7. begin
  8.   with TOpenPictureDialog.Create(Form1) do
  9.   begin
  10.     if Execute then
  11.     begin
  12.       if not(Assigned(Image)) then
  13.       begin
  14.         Image                  := TImage.Create(Form1);
  15.         Image.Parent           := Form1;
  16.         Image.Left             := 50;
  17.         Image.Top              := 50;
  18.         Background             := TShape.Create(Form1);
  19.         Background.Parent      := Form1;
  20.         Background.Brush.Color := clBlack;
  21.         Background.Pen.Style   := psClear;
  22.         Background.Left        := Image.Left - ImageBorder;
  23.         Background.Top         := Image.Top  - ImageBorder;
  24.         Background.SendToBack;
  25.       end;
  26.       Image.Picture.LoadFromFile(FileName);
  27.       Image.Height      := Image.Picture.Height;
  28.       Image.Width       := Image.Picture.Width;
  29.       Background.Height := Image.Picture.Height + 2*ImageBorder;
  30.       Background.Width  := Image.Picture.Width  + 2*ImageBorder;
  31.     end;
  32.     Free;
  33.   end;
  34. end;

  • Line #3 is for the thickness of the border.
  • Line #24 is to make sure the background behind the image.
« Last Edit: June 17, 2022, 02:13:34 pm by Handoko »

Roman

  • New Member
  • *
  • Posts: 21
Re: Image background
« Reply #2 on: June 17, 2022, 02:32:50 pm »
Thank you. It works.

Handoko

  • Hero Member
  • *****
  • Posts: 4712
  • My goal: build my own game engine using Lazarus
Re: Image background
« Reply #3 on: June 17, 2022, 02:40:05 pm »
This below is a different solution, it uses a temporary TPicture:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const
  3.   Border = 5;
  4. const
  5.   Image: TImage = nil;
  6. var
  7.   Temp: TPicture;
  8. begin
  9.   with TOpenPictureDialog.Create(Form1) do
  10.   begin
  11.     if Execute then
  12.     begin
  13.       if not(Assigned(Image)) then
  14.       begin
  15.         Image        := TImage.Create(Form1);
  16.         Image.Parent := Form1;
  17.         Image.Left   := 50;
  18.         Image.Top    := 50;
  19.       end;
  20.       Temp := TPicture.Create;
  21.       Temp.LoadFromFile(FileName);
  22.       Image.Height := Temp.Height + 2*Border;
  23.       Image.Width  := Temp.Width  + 2*Border;
  24.       Image.Picture.Clear;
  25.       Image.Canvas.CopyRect(
  26.         Rect(Border, Border, Temp.Width + Border, Temp.Height + Border),
  27.         Temp.Bitmap.Canvas,
  28.         Rect(0, 0, Temp.Width, Temp.Height)
  29.         );
  30.       Temp.Free;
  31.     end;
  32.     Free;
  33.   end;
  34. end;

 

TinyPortal © 2005-2018