isola 1 Posted July 1, 2023 I have a class that is inherited from TComponent. The class have some published fields. It is possible in runtime using the bundle below save the class to a file. MemStream.WriteComponent(self); FileStream := TFileStream.Create(FStorageName, fmCreate); ObjectBinaryToText(MemStream, FileStream); Then it's possible using below code to read saved data back into an object directly. ObjectTextToBinary(FileStream, MemStream); Everything works well for simple types(Int, string, boolean). This serialization works as well for some other types (TStringList, TCollection). What about TImage class? It seems published TImage is ignored. But TImage is also the descendant of TComponent. Does Delphi use the same approach to save TForm to dfm-files? Whether any extra effort should be made to serialize TImage? Share this post Link to post
Uwe Raabe 2057 Posted July 1, 2023 Make the Owner of that TImage instance your component and set csSubComponent in the TImages ComponentStyle. That means, the image should be created like this: FImage := TImage.Create(Self); FImage.SetSubComponent(True); 2 Share this post Link to post
programmerdelphi2k 237 Posted July 1, 2023 (edited) 1 hour ago, isola said: Does Delphi use the same approach to save TForm to dfm-files or more simple way; you can use only "Memory streams instead save on file" procedure TForm1.Button1Click(Sender: TObject); var LMemStream: TMemoryStream; LStrStream: TStringStream; begin LMemStream := TMemoryStream.Create; try LMemStream.WriteComponent(Image1); LMemStream.SaveToFile('myTImage.bin'); // LStrStream := TStringStream.Create; try LMemStream.Position := 0; ObjectBinaryToText(LMemStream, LStrStream); LStrStream.Position := 0; Memo1.Text := LStrStream.DataString; // showing the propers finally LStrStream.Free; end; // Image1.Picture.Bitmap := nil; // just for test... finally LMemStream.Free; end; end; procedure TForm1.Button2Click(Sender: TObject); var LMemStream: TMemoryStream; begin LMemStream := TMemoryStream.Create; try LMemStream.LoadFromFile('myTImage.bin'); LMemStream.ReadComponent(Image1); finally LMemStream.Free; end; end; Edited July 1, 2023 by programmerdelphi2k 2 Share this post Link to post
programmerdelphi2k 237 Posted July 1, 2023 (edited) ... Edited July 1, 2023 by programmerdelphi2k 1 Share this post Link to post
isola 1 Posted July 1, 2023 FImage := TImage.Create(Self); FImage.SetSubComponent(True); It works. Uwe Raabe, thank you very much! Share this post Link to post