Recent

Author Topic: insert image from file at position on a canvas  (Read 14472 times)

T-bear

  • Full Member
  • ***
  • Posts: 160
insert image from file at position on a canvas
« on: May 27, 2011, 10:57:25 pm »
Hi all
Is it possible to insert a image from a file in a position on the canvas of for eksample form1? If yes, how can i do that?
Thanks for your help.

eny

  • Hero Member
  • *****
  • Posts: 1648
Re: insert image from file at position on a canvas
« Reply #1 on: May 27, 2011, 11:08:38 pm »
Hi all
Is it possible to insert a image from a file in a position on the canvas of for eksample form1? If yes, how can i do that?
Thanks for your help.

The easiest way probably is to put a TImage on the form anywhere you like.
And load an image in there (statically or dynamically).
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

Dibo

  • Hero Member
  • *****
  • Posts: 1057
Re: insert image from file at position on a canvas
« Reply #2 on: May 27, 2011, 11:53:14 pm »
Hi all
Is it possible to insert a image from a file in a position on the canvas of for eksample form1? If yes, how can i do that?
Thanks for your help.

You mean something like this? "FormPaint" method is Form.OnPaint event.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.     procedure FormDestroy(Sender: TObject);
  17.     procedure FormPaint(Sender: TObject);
  18.   private
  19.     { private declarations }
  20.     FImage: TPicture;
  21.   public
  22.     { public declarations }
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. begin
  36.   FImage := TPicture.Create;
  37.   FImage.LoadFromFile('3d.png');
  38. end;
  39.  
  40. procedure TForm1.FormDestroy(Sender: TObject);
  41. begin
  42.   FImage.Free;
  43. end;
  44.  
  45. procedure TForm1.FormPaint(Sender: TObject);
  46. begin
  47.   Self.Canvas.Draw(5,5,FImage.Graphic);
  48. end;
  49.  
  50. end.    
  51.  

eny

  • Hero Member
  • *****
  • Posts: 1648
Re: insert image from file at position on a canvas
« Reply #3 on: May 28, 2011, 12:10:31 am »
You mean something like this? "FormPaint" method is Form.OnPaint event.
You make it more difficult then it should be.
I mentioned TImage for a reason:
Code: Pascal  [Select][+][-]
  1.   with TImage.Create(self) do
  2.   begin
  3.     Parent  := self;
  4.     Picture.LoadFromFile('image.jpg');
  5.     Left    := 20;
  6.     Top     := 20;
  7.     Stretch := true;
  8.     /// other properties like width, height etc.
  9.   end;
Once created with owner and parent set to 'self' there is no need to destroy or paint it. It's all handled automatically.
« Last Edit: May 28, 2011, 12:12:07 am by eny »
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

Dibo

  • Hero Member
  • *****
  • Posts: 1057
Re: insert image from file at position on a canvas
« Reply #4 on: May 28, 2011, 12:17:23 am »
Yes, I know that :) . I just answered to his question, because maybe he have some reasons to draw image exactly on the form canvas. We don't know that.

 

TinyPortal © 2005-2018