Squall_FF8 1 Posted 6 hours ago 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
Remy Lebeau 1587 Posted 6 hours ago (edited) 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 4 hours ago by Remy Lebeau 1 1 Share this post Link to post
Squall_FF8 1 Posted 6 hours ago 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
Uwe Raabe 2141 Posted 5 hours ago If Image1.Picture.Graphic is not of type TBitmap, referencing Image1.Picture.Bitmap will clear the current content and create an empty TBitmap. 2 1 Share this post Link to post
Remy Lebeau 1587 Posted 4 hours ago (edited) 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 4 hours ago by Remy Lebeau Share this post Link to post
steelha 0 Posted 2 hours ago 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
Remy Lebeau 1587 Posted 2 hours ago 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
steelha 0 Posted 2 hours ago 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