Ok leider bekomme ich beim auslesen immer den Fehler Zugriffsverletzung.
Delphi-Quellcode:
type
TProfile = class(TObject)
id: String;
name: Ansistring;
image: TMemorystream;
procedure LoadFromStream(AStream: TStream);
procedure SaveToStream(AStream: TStream);
end;
...
procedure TProfile.SaveToStream(AStream: TStream);
var
iLen: Integer;
begin
{String ID}
iLen := Length(ID);
AStream.Write(iLen, SizeOf(iLen));
AStream.Write(PChar(ID)^, iLen);
{String Name}
iLen := Length(Name);
AStream.Write(iLen, SizeOf(iLen));
AStream.Write(PChar(Name)^, iLen);
{memorystream image}
iLen := Image.Size;
AStream.Write(iLen, SizeOf(iLen));
Image.Position := 0;
AStream.CopyFrom(Image, iLen)
end;
procedure TProfile.LoadFromStream(AStream: TStream);
var
iLen: Integer;
begin
{String ID}
AStream.Read(iLen, SizeOf(iLen));
SetLength(ID, iLen);
AStream.Read(PChar(ID)^, iLen);
{String Name}
AStream.Read(iLen, SizeOf(iLen));
SetLength(Name, iLen);
AStream.Read(PChar(Name)^, iLen);
{memorystream image}
AStream.Read(iLen, SizeOf(iLen));
Image.Clear;
Image.CopyFrom(AStream, iLen);
end;
Zum schreiben das sieht so aus:
Delphi-Quellcode:
Profile:= TProfile.Create;
Profile.id:='2';
Profile.name:='huhu';
Profile.image:= TMemorystream.Create;
Profile.image.LoadFromFile(ExtractFilePath(ParamStr(0))+'Images/test.png');
Stream:= TMemoryStream.Create;
Profile.SaveToStream(Stream);
Und an anderer Stelle lese ich das so wieder aus:
Delphi-Quellcode:
Profile1:= TProfile.Create;
Stream.Position:=0;
Profile1.LoadFromStream(stream);
WriteLn(Profile1.id);
WriteLn(Profile1.name);
Profile1.image:= TMemorystream.Create;
Profile1.image.SaveToFile('C:\test.png');
Oder kann ich vielleicht direkt den Typ image in den Stream laden, aber da weiß ich nicht welchen typ ich nehmen kann.