Jump to content
Squall_FF8

Is TImage empty

Recommended Posts

Hey Guys,

I have TImage that in run-time I can load with png or jpg.
1. How to check is the TImage empty (aka no image loaded?

2. How to erase the loaded image? (when an image is loaded)

Share this post


Link to post
2 hours ago, Squall_FF8 said:

1. How to check is the TImage empty (aka no image loaded?

Check the TImage.Picture.Graphic and TGraphic.Empty properties:

if (Image1.Picture.Graphic = nil) or Image.Picture.Graphic.Empty then
2 hours ago, Squall_FF8 said:

2. How to erase the loaded image? (when an image is loaded)

You can assign nil to the TImage.Picture property:

Image1.Picture := nil;
// same as:
// Image1.Picture.Assign(nil);

Or to its Graphic property:

Image1.Picture.Graphic := nil;

 

Edited by Remy Lebeau
  • Like 1
  • Thanks 1

Share this post


Link to post

Thank you @Remy Lebeau!

 

BTW do you know, why Image1.Picture.Bitmap.Empty clears the image? It is suppose the return a value, not to destroy an image

Share this post


Link to post

If Image1.Picture.Graphic is not of type TBitmap, referencing Image1.Picture.Bitmap will clear the current content and create an empty TBitmap.

  • Like 2
  • Thanks 1

Share this post


Link to post

Yes, and this is documented behavior:

https://docwiki.embarcadero.com/Libraries/en/Vcl.Graphics.TPicture.Bitmap

Quote

Use Bitmap to reference the picture object when it contains a bitmap. If Bitmap is referenced when the picture contains a Metafile or Icon graphic, the graphic won't be converted (Types of Graphic Objects). Instead, the original contents of the picture are discarded and Bitmap returns a new, blank bitmap.

Where it says "a Metafile or Icon graphic", it really means "any non-Bitmap graphic" instead.

 

Use the TPicture.Graphic property when you need to access as-is whatever TGraphic descendant is currently loaded in the TPicture.  Use the TPicture.Bitmap property instead when you specifically need a TBitmap.

Edited by Remy Lebeau

Share this post


Link to post

I hope this code help you

procedure LoginImage(const RutaBase: string);
var
  filename : string;
  ImagenBMP: TBitmap;
  ImagenJPG: TJPEGImage;
  ImagenPNG: TPngImage;
  filefind: Boolean;
begin

  ImagenBMP := nil;
  ImagenJPG := nil;
  ImagenPNG := nil;
  filefind:= False;

try
    // Intentar cargar como BMP
    filename:= IncludeTrailingPathDelimiter(RutaBase) + 'flogin.bmp';
    if FileExists(filename) then
    begin
      ImagenBMP := TBitmap.Create;
      ImagenBMP.LoadFromFile(filename);
      imgFLogin.Picture.Assign(ImagenBMP);
      filefind := True;
    end
    else
    begin
        Showmessage('File not fouund')
    end;

  finally
    // Liberar los objetos si fueron creados
    ImagenBMP.Free;
    ImagenJPG.Free;
    ImagenPNG.Free;
  end;

end;

 

just pass the file path.  Can change Tbimap.create for TJPEGImage.Create; or  TPngImage.Create;,

imgFLogin its the image timage visual component you want load the image 

Share this post


Link to post
2 minutes ago, steelha said:

I hope this code help you

...

just pass the file path.  Can change Tbimap.create for TJPEGImage.Create; or  TPngImage.Create;,

imgFLogin its the image timage visual component you want load the image 

How does that code have anything to do with the discussion at hand?

 

Also, why would you not simply use TImage.Picture.LoadFromFile() and let it handle everything for you?

procedure LoginImage(const RutaBase: string);
var
  filename : string;
begin
  // Intentar cargar como BMP
  filename := IncludeTrailingPathDelimiter(RutaBase) + 'flogin.bmp';
  if FileExists(filename) then
  begin
    imgFLogin.Picture.LoadFromFile(filename);
  end
  else
  begin
    ShowMessage('File not found')
  end;
end;

 

Share this post


Link to post

Sorry dont pass all the code

 

 imgFLogin.Picture.Assign(nil);

then i call the procedure LoginImage(ExtractFilePath(Application.ExeName))

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×