Recent

Author Topic: Changing pictures after reaching value on the TImage component  (Read 11373 times)

gromar

  • New member
  • *
  • Posts: 7
Hello!

I'd like to change picture loaded into Image1 - from one called greenseat.gif to second called redseat.gif after the Integer value will be <=20.

For example: I have an icon of green seat in bus and it shows number of seat which are free (overall there will be 30 seats), if 20 ppl will sit on those seats the icon should change colour to red (it should load the different image representing red seat).

Is there any way to do it? Thank you in advance for any tips : )

mm7

  • Full Member
  • ***
  • Posts: 193
  • PDP-11 RSX Pascal, Turbo Pascal, Delphi, Lazarus
Re: Changing pictures after reaching value on the TImage component
« Reply #1 on: March 12, 2015, 05:18:13 pm »
var gifR,gifG : TGIFImage;
...
gifR:=TGIFImage.Create;
gifR.LoadFromFile('redseat.gif');
gifG:=TGIFImage.Create;
gifG.LoadFromFile('grnseat.gif');
....
if seats <= 20
then Image1.Picture.Assign(gifR);
else Image1.Picture.Assign(gifG);

Basile B.

  • Guest
Re: Changing pictures after reaching value on the TImage component
« Reply #2 on: March 12, 2015, 06:10:19 pm »
not good, you create two object instances while only one is needed. And there is a syntax error (semi-colon after the if then statement)

Code: [Select]
var
  gif: TGIFImage;
const
  names: array[boolean] of string = ['redseat.gif', 'grnseat.gif'];
...
gif := TGIFImage.Create;
gif.LoadFromFile(names[seats <= 20]);
Image1.Picture.Assign(gif);
...
« Last Edit: March 12, 2015, 06:12:21 pm by Basile B. »

mm7

  • Full Member
  • ***
  • Posts: 193
  • PDP-11 RSX Pascal, Turbo Pascal, Delphi, Lazarus
Re: Changing pictures after reaching value on the TImage component
« Reply #3 on: March 12, 2015, 06:25:52 pm »
 :D
>not good, you create two object instances while only one is needed.
And I even do not destroy them after!

>And there is a syntax error (semi-colon after the if then statement)
And there is another syntax error : .... - Dot found when END is expected :)

By the way,
Code: [Select]
names[seats <= 20]Uses True and False as numeric indexes 1 and 0.
Not very good from Pascal's concept of strict types . It is too C-ish :\
:)
« Last Edit: March 12, 2015, 06:30:19 pm by mm7 »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Changing pictures after reaching value on the TImage component
« Reply #4 on: March 12, 2015, 07:06:47 pm »
I must be missing something here. Why not call Image1.Picture.LoadFromFile directly instead of using TGIFImage?

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: Changing pictures after reaching value on the TImage component
« Reply #5 on: March 12, 2015, 07:16:32 pm »
Quote
const
  names: array[boolean] of string = ['redseat.gif', 'grnseat.gif'];
....
Uses True and False as numeric indexes 1 and 0.
Not very good from Pascal's concept of strict types . It is too C-ish :\

Nothing is "C-ish" with this declaration. The names array is declared to use boolean as index. Where did you read that an array index can only be an integer in Pascal?

mm7

  • Full Member
  • ***
  • Posts: 193
  • PDP-11 RSX Pascal, Turbo Pascal, Delphi, Lazarus
Re: Changing pictures after reaching value on the TImage component
« Reply #6 on: March 12, 2015, 08:03:17 pm »
sorry, I missed that part.

mm7

  • Full Member
  • ***
  • Posts: 193
  • PDP-11 RSX Pascal, Turbo Pascal, Delphi, Lazarus
Re: Changing pictures after reaching value on the TImage component
« Reply #7 on: March 12, 2015, 08:09:58 pm »
I must be missing something here. Why not call Image1.Picture.LoadFromFile directly instead of using TGIFImage?

I did not try. GIF is not documented here.
http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/tpicture.html
http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/tbitmap.html
Quote
TBitmap is the data of an image. The image can be loaded from a file, stream or resource in .bmp (windows bitmap format) or .xpm (XPixMap format)
« Last Edit: March 12, 2015, 08:13:11 pm by mm7 »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Changing pictures after reaching value on the TImage component
« Reply #8 on: March 12, 2015, 08:24:31 pm »
The description in TPicture.LoadFromFile page:
Quote
LoadFromFile - Reads a picture from disk. The TGraphic class created is determined by the file extension of the file.

And the source code:
Code: [Select]
constructor TPicFileFormatsList.Create;
begin
  inherited Create;
  // add by priority of use in LCL/IDE
  Add(TPortableNetworkGraphic.GetFileExtensions, rsPortableNetworkGraphic, TPortableNetworkGraphic);
  Add(TPixmap.GetFileExtensions, rsPixmap, TPixmap);
  Add(TBitmap.GetFileExtensions, rsBitmaps, TBitmap);
  Add(TCursorImage.GetFileExtensions, rsCursor, TCursorImage);
  Add(TIcon.GetFileExtensions, rsIcon, TIcon);
  Add(TIcnsIcon.GetFileExtensions, rsIcns, TIcnsIcon);
  Add(TJpegImage.GetFileExtensions, rsJpeg, TJpegImage);
  Add(TTiffImage.GetFileExtensions, rsTiff, TTiffImage);
  Add(TGIFImage.GetFileExtensions, rsGIF, TGIFImage);   //<------------- Here we go
  Add(TPortableAnyMapGraphic.GetFileExtensions, rsPortablePixmap, TPortableAnyMapGraphic);
end;

mm7

  • Full Member
  • ***
  • Posts: 193
  • PDP-11 RSX Pascal, Turbo Pascal, Delphi, Lazarus
Re: Changing pictures after reaching value on the TImage component
« Reply #9 on: March 12, 2015, 10:00:50 pm »
Well, in code there it is. But not in official docs. In other systems such thing is called "undocumented feature" and it is not recommended to rely on it and use in programs, because it can change anytime.
I am new to Lazarus, how is it here? Should one rely on a feature that is not yet officially documented?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Changing pictures after reaching value on the TImage component
« Reply #10 on: March 12, 2015, 10:18:50 pm »
What exactly in the documentation gave the impression that Image1.Picture.LoadFromFile will not work for *.gif images?

gromar

  • New member
  • *
  • Posts: 7
Re: Changing pictures after reaching value on the TImage component
« Reply #11 on: March 13, 2015, 01:21:22 pm »
Hello again,

I've made it - it works like in example I wrote. Next thing I'd like to achieve is to make proper counters which will show separately how many seats are free (counting green images of seats) or not (red image of seat). So far my code looks like this (I'm testing it with one seat):
Code: [Select]
procedure TForm1.Edit1Change(Sender: TObject);
var gifR,gifG : TGIFImage;
  siedzenie : integer;
begin
    siedzenie := Length(Edit1.Text);
    Label1.Caption := IntToStr(siedzenie);

gifR:=TGIFImage.Create;
gifR.LoadFromFile('red.gif');
gifG:=TGIFImage.Create;
gifG.LoadFromFile('grn.gif');
    if siedzenie <= 20 then Image1.Picture.Assign(gifG)
    else Image1.Picture.Assign(gifR);
end;

mm7

  • Full Member
  • ***
  • Posts: 193
  • PDP-11 RSX Pascal, Turbo Pascal, Delphi, Lazarus
Re: Changing pictures after reaching value on the TImage component
« Reply #12 on: March 13, 2015, 03:43:42 pm »
What exactly in the documentation gave the impression that Image1.Picture.LoadFromFile will not work for *.gif images?

GIF is not there.
And not here as well. http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/tpicture.html
Other formats are.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Changing pictures after reaching value on the TImage component
« Reply #13 on: March 13, 2015, 04:39:48 pm »
What exactly in the documentation gave the impression that Image1.Picture.LoadFromFile will not work for *.gif images?

GIF is not there.
And not here as well. http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/tpicture.html
Other formats are.

Neither link is meant to give a comprehensive list of accepted extensions/types, but Graphics.GetGraphicClassForFileExtension can give a class that supports a specific extension if available. And passing TGraphic to Graphics.GraphicFilter will provide a list of file extensions, on my system at this time I get:
Quote
*.png;*.xpm;*.bmp;*.cur;*.ico;*.icns;*.jpeg;*.jpg;*.jpe;*.jfif;*.tif;*.tiff;*.gif;*.pbm;*.pgm;*.ppm

With a few lines of code:
Code: [Select]
var
  GC, oldGC: TGraphicClass;
  sl: TStringList;
  AllFilters: String;
  aFilter, aExt: String;
begin
  AllFilters := GraphicFileMask(TGraphic);
  Memo1.Lines.Add(AllFilters);

  sl := TStringList.Create;
  try
    sl.Delimiter := ';';
    sl.DelimitedText := AllFilters;
    for aFilter in sl do
    begin
      aExt := copy(aFilter, 3, Length(aFilter));
      GC := GetGraphicClassForFileExtension(aExt);
      if oldGC <> GC then
      begin
        Memo1.Lines.Add(GraphicFilter(GC));
        oldGC := GC;
      end;
    end;
  finally
    sl.Free;
  end;
end;

it could be turned to:
Quote
Portable Network Graphic (*.png)|*.png
Pixmap (*.xpm)|*.xpm
Bitmaps (*.bmp)|*.bmp
Cursor (*.cur)|*.cur
Icon (*.ico)|*.ico
Mac OS X Icon (*.icns)|*.icns
Joint Picture Expert Group (*.jpeg;*.jpg;*.jpe;*.jfif)|*.jpeg;*.jpg;*.jpe;*.jfif
Tagged Image File Format (*.tif;*.tiff)|*.tif;*.tiff
Graphics Interchange Format (*.gif)|*.gif
Portable PixMap (*.pbm;*.pgm;*.ppm)|*.pbm;*.pgm;*.ppm

More over the log of the source code shows that Image1.Picture.LoadFromFile can handle *.gif for over four years now, thanks to a patch by José Mejuto.

mm7

  • Full Member
  • ***
  • Posts: 193
  • PDP-11 RSX Pascal, Turbo Pascal, Delphi, Lazarus
Re: Changing pictures after reaching value on the TImage component
« Reply #14 on: March 13, 2015, 07:25:25 pm »
Neither link is meant to give a comprehensive list of accepted extensions/types, but Graphics.GetGraphicClassForFileExtension can give a class ...
Thank you for the example. I see that lots of formats are actually supported.
And if this support has been added time ago, and well tested, then why not to update the documentation?

What I am trying to say, in most of mature systems there also some features appear before they get documented. But they are treated as "undocumented features", that are not recommended for use, especially for production use, until they are released and documented.
From what you are saying I see that this approach does not apply to FPC/Lazarus. Then how to be sure that some new cool feature I use today, will not change tomorrow, and something will stop compile or work?

 

TinyPortal © 2005-2018