Changing pictures by clicking on the TImage component - Delphi


I want to <strong>change picture</strong> loaded into <strong>Image1</strong> - from one called <strong>1active.png</strong> to second called <strong>1inactive.png</strong> and vice versa <strong>by clicking</strong> on the Image1 component.

Is there any way to do it?



<ul><li>1 boolean variable </li> <li>1 TImage component</li> <li>1 line added to form OnCreate event handler</li> <li>1 TImage.OnClick event handler</li> </ul>


1) Create a boolean variable in the private section of your form declaration. For this example, call this variable fImageActive.

private fImageActive : boolean;

2) From the form designer, drop (or select an existing) TImage component on the form and select the onClick event, and double click to switch into editing. Then add the following code:

fImageActive := not fImageActive; if fImageActive then Image1.Picture.LoadFromFile('1active.png') else Image1.Picture.LoadFromFile('1inactive.png')

3) From the form designer, find the picture property and click the ellipsis to load the 1active.png file for starters.

4) Click on the form, find the event named "OnCreate", double click and add the following code (this will set the initial state):

fImageActive := true;


I assume you are using D2009 as you can load a png file in Image1.

If you don't want to distribute your images along with your application (as skamradt's solution implies), you have to include them as resources:<br /> Add the png to your project: Use menu "<strong>Project | Resources...</strong>" and add the files.<br /> Name them accordingly to get something like:

1active.png RCData PngImage_Active 1inactive.png RCData PngImage_Inactive

In your Click event, you'll need some state indicator to know which one to display (like skamradt).<br /> I used the Image1.hint to tell which image is loaded (quick and dirty, for demo purpose).

Use the LoadImgFromPngResource proc below to load the appropriate png image:

procedure LoadImgFromPngResource(const AResName: string; DestImage: TImage); var png: TPngImage; begin png := TPngImage.Create; try png.LoadFromResourceName(HInstance, AResName); DestImage.Picture.Assign(png); finally png.Free; end; end; procedure TForm4.Image1Click(Sender: TObject); begin if Image1.Hint <> 'Active' then begin LoadImgFromPngResource('PngImage_Active', Image1); Image1.Hint := 'Active'; end else begin LoadImgFromPngResource('PngImage_Inactive', Image1); Image1.Hint := 'Inactive'; end; end;


You have to load both images into the resource file of your project, and in runtime you can read the pics from the resource file and load them into the timage on the OnClick. The version of Delphi I'm currently using (6) does not have native support for PNG files, but I think latter version do


  • Can I include a Generic type parameter in a lamba expression? (VB.NET 2010)
  • How to add a resource file to Visual Studios so that it's visible in “Select Resource” dialog?
  • SCSS Language Injection in Pycharm
  • How to work with Master Page that is attached to the page via the page's basepage?
  • How to change default stop edit behavior in jtable
  • goJS dropdown remove items
  • write text on image and show it to a imageview
  • Certain Arabic text gets incorrectly shown while other Arabic text gets showed normally?
  • How to change placeholder text in an autocomplete activity of android google place?
  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • Hide HTML elements without javascript, only CSS
  • Copy to all folders batch file?
  • Use of this Javascript
  • Moving mysql files across servers
  • Linq Objects Group By & Sum
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • What is Eclipse's Declaration View used for?
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Adding custom controls to a full screen movie
  • jquery mobile loadPage not working
  • SVN: Merging two branches together
  • How to format a variable of double type
  • Data Validation Drop Down Box Arrow Disappearing
  • Buffer size for converting unsigned long to string
  • Load html files in TinyMce
  • Acquiring multiple attributes from .xml file in c#
  • How do you join a server to an Active Directory (domain)?
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Android Google Maps API OnLocationChanged only called once
  • How does Linux kernel interrupt the application?
  • How can I remove ASP.NET Designer.cs files?
  • Busy indicator not showing up in wpf window [duplicate]
  • UserPrincipal.Current returns apppool on IIS
  • Why do underscore prefixed variables exist?
  • java string with new operator and a literal