Write and Read a Record containing a bitmap into a stream - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Write and Read a Record containing a bitmap into a stream

DWDuckDWDuck Posts: 31Member
Good day I have this problem and need a solution I have a records like follows:

MyBitmapRecord = Record
Name: String[100];
MyBitmap : TBitmap;
Age : Integer;
End;

I require a record like above to be saved into a streamfile...

my Code is currently like this

Procedure writeToFile(Fname: String;Rec: MyBitmapRecord);
Var
Stream: TStream;
bStream: TMemoryStream;
Len : LongInt;
Begin

stream := TFileStream.Create(Fname,fmCreate);
bStream := TMemoryStream.Create;
try
bstream.Clear;
Len := Length(Rec.Name);
stream.WriteBuffer(len,sizeof(len));
stream.WriteBuffer(Rec.Name[1],sizeof(Rec.Name));
stream.WriteBuffer(Rec.Age,sizeof(Menu1.Age));
Rec.Mybitmap.savetoStream(bstream);
aStream.Position := 0;
len := bstream.Size;
stream.WriteBuffer(len,sizeof(len));
stream.Copyfrom(bstream,0);
finally
stream.Free;
bStream.Free;

End;

Procedure Readfromfile(FName: String;Var Rec: Mybitmaprecord);
Var
Len : LongInt;
Stream: TStream;
bStream: TMemoryStream;
Begin
stream := TFileStream.Create(Fname,fmopenRead);
bstream := TMemoryStream.Create;
try
stream.ReadBuffer(len,sizeof(len));
SetLength(Rec.Name,len);
stream.ReadBuffer(Rec.Name[1],len);
stream.ReadBuffer(Rec.Age,sizeof(Rec.age));
Rec.MyBitmap := TBitmap.Create;
stream.ReadBuffer(len,sizeof(len));
bstream.size := 0;
bstream.CopyFrom(stream,len);
bstream.position := 0;
Rec.MyBitmap.LoadFromStream(bStream);
Finally
Stream.free;
bStream.free;
End;

The writetofile procedure appears to work... However when I call the readfromfile procedure I get an error stating no a valid bitmap file... Can someone help me URGENTLY plz
Darkwing Duck aka DWduck signing off :)
Sign In or Register to comment.