Recent

Author Topic: How to insert an image  (Read 2549 times)

iDavis

  • Newbie
  • Posts: 1
How to insert an image
« on: September 19, 2018, 10:09:30 pm »
Hi all,

I´m currently working on my new program, it is quiet simple, but I got stuck with inserting an image. What is the syntax to add a picture from the file for button click? Thanks in advance.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: How to insert an image
« Reply #1 on: September 19, 2018, 11:36:20 pm »
I guess a Timage would work...

You can even use it as a button if you wish...

 Then there is the Tbitbtn  button which you can assign an image to it from a TImageList.
The only true wisdom is knowing you know nothing

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to insert an image
« Reply #2 on: September 19, 2018, 11:40:06 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   if OpenPictureDialog1.Execute then
  4.     Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
  5. end;

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to insert an image
« Reply #3 on: September 20, 2018, 12:08:43 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   if OpenPictureDialog1.Execute then
  4.     Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
  5. end;

Better do it like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   if OpenPictureDialog1.Execute then
  4.     Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
  5. end;
so as to be able to load any supported format. It's also more Delphi-compatible, IIRC.

Incidentally, in HowardPC's mini-code:
  • procedure TForm1.Button1Click(); is the handler of a TButton (Button1) OnClick event;
  • OpenPictureDialog1 is a TOpenPictureDialog
  • Image1 is a TImage
We are suppossing that you're using Lazarus and that you know how to place components on the form, change properties, add event handlers, etc. If this is not the case, please tell us and we will adjust the "level" of our responses.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018