Streaming Objects to File ?

I am using Delphi 2010. I am creating a small application to create a Quiz. I have created my own unit (UntCommon) which includes the following classes (Objects)

TQuiz
TQuestions
TQuestion

Question 1: Why am I getting the error message

"[DCC Error] UntCommon.pas(46): E2188 Published property 'Items' cannot be of type ARRAY"

for the "Items" property  of the TQuestions class?

Question 2: How can I save a Quiz to file (possibly stream) it in somekind of binary format. I would like the answer for each question to be encrypted ( I will work on that later), but for now, im not worried about anything being hacked or readable in the file....but it would be nice to be able to stream it as binary.

I have created two procedures in the TQuiz class (commented out).

Procedure SaveToFile(FileName: String);
Procedure LoadFromFile(FileName: String);

Question 3: Do you see anything else wrong with my code? Most of it was pieced together from help i found using google.

Thanks

unit UntCommon;

interface

uses Classes, Contnrs, Graphics;

type

TQuizType = (qtTrueFalse, qtMultipleChoice, qtFillBlanks, qtAll);

TQuestion = class(TObject)
 private
  fQuestion: String;
  fAnswer: String;
  fAnswers: TStringList;
  fImage: TPicture;
  procedure SetQuestion(Value: String);
  procedure SetAnswer(Value: String);
  procedure SetAnswers(Value: TStrings);
  procedure SetImage(Value: TPicture);
  function GetQuestion: String;
  function GetAnswer: String;
  function GetAnswers: TStrings;
  function GetImage: TPicture;
 public
  constructor Create;
  destructor Destroy;override;
 published
  property Question: String read GetQuestion write SetQuestion;
  property Answer: String read GetAnswer write SetAnswer;
  property Answers: TStrings read GetAnswers write SetAnswers;
  property Image: TPicture read GetImage write SetImage;
end;

TQuestions = class(TObjectList)
 private
  function GetItems(Index: Integer): TQuestion;
  procedure SetItems(Index: Integer; value: TQuestion);
 public
  constructor Create;
  function Add(value: TQuestion): Integer;
  function IndexOf(value: TQuestion): Integer;
  procedure Insert(Index: Integer; value: TQuestion);
  function Remove(value: TQuestion): Integer;
 published
  property Items[Index: Integer]: TQuestion read GetItems write SetItems; default;
end;

TQuiz = class(TObject)
 private
  fTitle: String;
  fType: TQuizType;
  fAuthor: String;
  fQuestions: TQuestions;
  procedure SetTitle(Value: String);
  procedure SetType(Value: TQuizType);
  procedure SetAuthor(Value: String);
  procedure SetQuestions(Value: TQuestions);
  function GetTitle: String;
  function GetType: TQuizType;
  function GetAuthor: String;
  function GetQuestions: TQuestions;
 public
  constructor Create;
  destructor Destroy;override;
  //procedure SaveToFile(FileName: String);
  //procedure LoadToFile(FileName: String);
 published
 property Title: String read GetTitle write SetTitle;
 property QuizType: TQuizType read GetType write SetType;
 property Author: String read GetAuthor write SetAuthor;
 property Questions: TQuestions read GetQuestions write SetQuestions;
end;

implementation

{********** TQuestion **********}

procedure TQuestion.SetQuestion(Value: String);
begin
 if fQuestion <> Value then
  fQuestion:= Value;
end;

procedure TQuestion.SetAnswer(Value: String);
begin
 if fAnswer <> Value then
  fAnswer:= Value;
end;

procedure TQuestion.SetAnswers(Value: TStrings);
begin
  fAnswers.Assign(Value);
end;

procedure TQuestion.SetImage(Value: TPicture);
begin
 fImage.Assign(Value);
end;

function TQuestion.GetQuestion: String;
begin
 result:= fQuestion;
end;

function TQuestion.GetAnswer: String;
begin
 result:= fAnswer;
end;

function TQuestion.GetAnswers: TStrings;
begin
 result.Assign(fAnswers);
end;

function TQuestion.GetImage: TPicture;
begin
 result.Assign(fImage);
end;

constructor TQuestion.Create;
begin
 inherited Create;
 fAnswers:= TSTringList.Create;
 fImage:= TPicture.Create;
 fImage:= nil;
end;

destructor TQuestion.Destroy;
begin
 fImage.Free;
 fAnswers.Free;
 inherited Destroy;
end;

{********** TQUESTIONS **********}

constructor TQuestions.Create;
begin
 inherited Create(true);
end;

function TQuestions.Add(value: TQuestion): Integer;
begin
 Result := inherited Add(value);
end;

function TQuestions.GetItems(Index: Integer): TQuestion;
begin
 Result := TQuestion(inherited Items[Index]);
end;

function TQuestions.IndexOf(value: TQuestion): Integer;
begin
 Result := inherited IndexOf(value);
end;

procedure TQuestions.Insert(Index: Integer; value: TQuestion);
begin
 inherited Insert(Index, value);
end;

function TQuestions.Remove(value: TQuestion): Integer;
begin
 Result := inherited Remove(value);
end;

procedure TQuestions.SetItems(Index: Integer; value: TQuestion);
begin
 inherited Items[Index] := value;
end;

{********** TQUIZ **********}

procedure TQuiz.SetTitle(Value: String);
begin
 if fTitle <> Value then
  fTitle:= Value;
end;

function TQuiz.GetTitle: String;
begin
 result:= fTitle;
end;

procedure TQuiz.SetType(Value: TQuizType);
begin
 if fType <> Value then
  fType:= Value;
end;

function TQuiz.GetType: TQuizType;
begin
 result:= fType;
end;

procedure TQuiz.SetAuthor(Value: String);
begin
 if fAuthor <> Value then
  fAuthor:= Value;
end;

function TQuiz.GetAuthor: String;
begin
 result:= fAuthor;
end;

procedure TQuiz.SetQuestions(Value: TQuestions);
begin
 fQuestions.Assign(Value);
end;

function TQuiz.GetQuestions: TQuestions;
begin
 result.assign(fQuestions);
end;

constructor TQuiz.Create;
begin
 inherited Create;
 fQuestions:= TQuestions.Create;
 fQuestions.OwnsObjects:= True;
end;

destructor TQuiz.Destroy;
begin
 fQuestions.Free;
 inherited Destroy;
end;

end.

Open in new window

DelphiRulezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ebob42Commented:
You cannot "publish" array properties. And since these are no components but classes, I would just change "published" to "public". Then the error message will be gone.
0
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
For the Q1
property Items[Index: Integer]
should be
property Items(Index: Integer)
0
DelphiRulezAuthor Commented:
Not sure if I should use Memory Stream or file streams. Which is the best?

procedure TQuiz.SaveToFile(FileName: String);
 var
 AStr: TFileStream;
begin
 AStr:= TFileStream.Create(FileName, fmOpenWrite);
 //
 AStr.Free;
end;

procedure TQuiz.LoadToFile(FileName: String);
 var
 AStr: TFileStream;
begin
 AStr:= TFileStream.Create(FileName, fmOpenRead);
 //
 AStr.Free;
end;
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

ebob42Commented:
I would use FileStream, or a plan TextFile that you can read and write as well to.
0
DelphiRulezAuthor Commented:
well, im still waiting on the SaveToFIle and LoadFrom File procedures

I may have them written before i get a answer from you experts (just from info im gathering from google and EE Search)


procedure TQuiz.SaveToFile(FileName: String);
 var
 AStr: TFileStream;
 I: Integer;
begin
 AStr:= TFileStream.Create(FileName, fmOpenWrite);
 WriteStreamInt(AStr, Questions.Count);
 for I:= 0 to  Questions.Count-1 do
 begin
  WriteStreamStr(AStr, TQuestion(Questions[I]).fQuestion);
    //write rest of question data here
 end;
 AStr.Free;
end;

procedure TQuiz.LoadFromFile(FileName: String);
 var
 AStr: TFileStream;
 QCount, I: Integer;
 AQuestion: TQuestion;
begin
 AStr:= TFileStream.Create(FileName, fmOpenRead);
 QCount:= ReadStreamInt(AStr);
 for I:= 0 to QCount-1 do
 begin
  AQuestion:= TQuestion.Create;
  AQuestion.fQuestion:= ReadStreamStr(AStr);
   //read rest of question data here
  Questions.Add(AQuestion);
 end;
 AStr.Free;
end;


procedure WriteStreamStr(Stream : TStream; Str : string);
var
  StrLen : integer;
begin
 StrLen := Length(Str);
 WriteStreamInt(Stream, StrLen);
 Stream.Write(Str[1], StrLen);
end;

function ReadStreamStr(Stream : TStream) : string;
var
  StrLen : integer;
begin
  StrLen := ReadStreamInt(Stream);
  if StrLen > -1 then begin
    SetLength(Result, StrLen);
    Stream.Read(Result[1], StrLen);
    end
  else
    Result := '';
end;


procedure WriteStreamInt(Stream : TStream; Num : integer);
begin
 Stream.Write(Num, SizeOf(Integer));
end;

function ReadStreamInt(Stream : TStream) : integer;
begin
 if Stream.Read(Result, SizeOf(Integer)) < SizeOf(Integer) then
  Result := -1;
end;

0
DelphiRulezAuthor Commented:
ok, all i have left to write is the Streaming of the TPicture (Not sure if this is do-able or not) .

of course, none of this is tested. Its just compiling so far.  Anyone see any other mistakes or problems i have not foreseen?????????  Can anyone write the code to stream a TPicture?

I need to be able to handle all registered image classes within Delphi (bmp, jpg, gif, png, ect).

Do i need to store an extension, size, ect in the stream????????



procedure TQuiz.SaveToFile(FileName: String);
 var
 AStr: TFileStream;
 I, J: Integer;
begin
 AStr:= TFileStream.Create(FileName, fmOpenWrite);
 WriteStreamStr(AStr, Title);
 WriteStreamInt(AStr, Integer(QuizType));
 WriteStreamStr(AStr, Author);
 WriteStreamInt(AStr, Questions.Count);
 for I:= 0 to  Questions.Count-1 do
 begin
  WriteStreamStr(AStr, TQuestion(Questions[I]).fQuestion);
  WriteStreamStr(AStr, TQuestion(Questions[I]).fAnswer);
  WriteStreamInt(AStr, TQuestion(Questions[I]).fAnswers.Count);
  for J:= 0 to TQuestion(Questions[I]).fAnswers.Count - 1 do
  begin
   WriteStreamStr(AStr, TQuestion(Questions[I]).fAnswers[J]);
  end;
  //write the TPicture to Stream
 end;
 AStr.Free;
end;

procedure TQuiz.LoadFromFile(FileName: String);
 var
 AStr: TFileStream;
 QCount, ACOunt, I: Integer;
 AQuestion: TQuestion;
  J: Integer;
begin
 AStr:= TFileStream.Create(FileName, fmOpenRead);
 Title:= ReadStreamStr(AStr);
 QuizType:= TQuizType(ReadStreamInt(AStr));
 Author:= ReadStreamStr(AStr);
 QCount:= ReadStreamInt(AStr);
 for I:= 0 to QCount-1 do
 begin
  AQuestion:= TQuestion.Create;
  AQuestion.fQuestion:= ReadStreamStr(AStr);
  AQuestion.fAnswer:= ReadStreamStr(AStr);
  ACount:= ReadStreamInt(AStr);
  for J := 0 to ACount - 1 do
  begin
   AQuestion.fAnswers.Add(ReadStreamStr(AStr));
  end;
  //read the TPicture from Stream
  Questions.Add(AQuestion);
 end;
 AStr.Free;
end;

function Encrypt(const S: String; Key: Word): String;
var
  I: byte;
begin
  SetLength(Result, Length(S));
  for I := 1 to Length(S) do
  begin
   Result[I] := char(byte(S[I]) xor (Key shr 8));
   Key := (byte(Result[I]) + Key) * C1 + C2;
  end;
end;

function Decrypt(const S: String; Key: Word): String;
var
  I: byte;
begin
 SetLength(Result, Length(S));
 for I := 1 to Length(S) do
 begin
  Result[I] := char(byte(S[I]) xor (Key shr 8));
  Key := (byte(S[I]) + Key) * C1 + C2;
 end;
end;

procedure WriteStreamStr(Stream : TStream; Str : string);
var
  StrLen : integer;
begin
 StrLen := Length(Str);
 WriteStreamInt(Stream, StrLen);
 Stream.Write(Str[1], StrLen);
end;

function ReadStreamStr(Stream : TStream) : string;
var
  StrLen : integer;
begin
  StrLen := ReadStreamInt(Stream);
  if StrLen > -1 then begin
    SetLength(Result, StrLen);
    Stream.Read(Result[1], StrLen);
    end
  else
    Result := '';
end;


procedure WriteStreamInt(Stream : TStream; Num : integer);
begin
 Stream.Write(Num, SizeOf(Integer));
end;

function ReadStreamInt(Stream : TStream) : integer;
begin
 if Stream.Read(Result, SizeOf(Integer)) < SizeOf(Integer) then
  Result := -1;
end;
0
DelphiRulezAuthor Commented:
Well, I decided to go with a TBitmap for my Image property for the time being, so i can move on. However, Im still looking for the capability to add (JPG, GIF, PNG, ect) all of the Delphi registered graphic images to my Unit.

Aside from that, if anyone see anything else that looks wrong, bad, ect. Please let me know. Im open to suggestions for code changes.

See attached code snipplet for finished unit
unit UntCommon;

interface

uses SysUtils, Classes, Contnrs, Graphics, JPEG;

const
  C1 = 52845;
  C2 = 22719;

type

TQuizType = (qtTrueFalse, qtMultipleChoice, qtFillBlanks, qtAll);

TQuestion = class(TObject)
 private
  fQuestion: String;
  fAnswer: String;
  fAnswers: TStringList;
  fImage: TBitmap;
  procedure SetQuestion(Value: String);
  procedure SetAnswer(Value: String);
  procedure SetAnswers(Value: TStrings);
  procedure SetImage(Value: TBitmap);
  function GetQuestion: String;
  function GetAnswer: String;
  function GetAnswers: TStrings;
  function GetImage: TBitmap;
 protected
 public
  constructor Create;
  destructor Destroy;override;
 published
  property Question: String read GetQuestion write SetQuestion;
  property Answer: String read GetAnswer write SetAnswer;
  property Answers: TStrings read GetAnswers write SetAnswers;
  property Image: TBitmap read GetImage write SetImage;
end;

TQuestions = class(TObjectList)
 private
  function GetItems(Index: Integer): TQuestion;
  procedure SetItems(Index: Integer; value: TQuestion);
 public
  constructor Create;
  function Add(value: TQuestion): Integer;
  function IndexOf(value: TQuestion): Integer;
  procedure Insert(Index: Integer; value: TQuestion);
  function Remove(value: TQuestion): Integer;
  procedure Delete(Index: Integer);
  property Items[Index: Integer]: TQuestion read GetItems write SetItems; default;
end;

TQuiz = class(TObject)
 private
  fPassword: String;
  fTitle: String;
  fType: TQuizType;
  fAuthor: String;
  fCreated: TDateTime;
  fQuestions: TQuestions;
  procedure SetPassword(Value: String);
  procedure SetTitle(Value: String);
  procedure SetType(Value: TQuizType);
  procedure SetAuthor(Value: String);
  procedure SetCreated(Value: TDateTime);
  procedure SetQuestions(Value: TQuestions);
  function GetPassword: String;
  function GetTitle: String;
  function GetType: TQuizType;
  function GetAuthor: String;
  function GetCreated: TDateTime;
  function GetQuestions: TQuestions;
 public
  constructor Create;
  destructor Destroy;override;
  procedure SaveToFile(FileName: String);
  procedure LoadFromFile(FileName: String);
 published
 property Password: String read GetPassword write SetPassword;
 property Title: String read GetTitle write SetTitle;
 property QuizType: TQuizType read GetType write SetType;
 property Author: String read GetAuthor write SetAuthor;
 property Created: TDateTime read GetCreated write SetCreated;
 property Questions: TQuestions read GetQuestions write SetQuestions;
end;

procedure WriteStreamInt(Stream : TStream; Num : integer);
function ReadStreamInt(Stream : TStream) : integer;

procedure WriteStreamStr(Stream : TStream; Str : string);
function ReadStreamStr(Stream : TStream) : string;

procedure WriteStreamBmp(Stream: TStream; Bitmap: TBitmap);
function ReadStreamBmp(Stream: TStream): TBitmap;

procedure WriteStreamDate(Stream : TStream; ADate : TDateTime);
function ReadStreamDate(Stream : TStream) : TDateTime;

function Decrypt(const S: String; Key: Word): String;
function Encrypt(const S: String; Key: Word): String;

implementation

{********** TQuestion **********}

procedure TQuestion.SetQuestion(Value: String);
begin
 if fQuestion <> Value then
  fQuestion:= Value;
end;

procedure TQuestion.SetAnswer(Value: String);
begin
 if fAnswer <> Value then
  fAnswer:= Value;
end;

procedure TQuestion.SetAnswers(Value: TStrings);
begin
  fAnswers.Assign(Value);
end;

procedure TQuestion.SetImage(Value: TBitmap);
begin
 fImage.Assign(Value);
end;

function TQuestion.GetQuestion: String;
begin
 result:= fQuestion;
end;

function TQuestion.GetAnswer: String;
begin
 result:= fAnswer;
end;

function TQuestion.GetAnswers: TStrings;
begin
 result.Assign(fAnswers);
end;

function TQuestion.GetImage: TBitmap;
begin
 result.Assign(fImage);
end;

constructor TQuestion.Create;
begin
 inherited Create;
 fAnswers:= TSTringList.Create;
 fImage:= TBitmap.Create;
 fImage:= nil;
end;

destructor TQuestion.Destroy;
begin
 fImage.Free;
 fAnswers.Free;
 inherited Destroy;
end;

{********** TQUESTIONS **********}

constructor TQuestions.Create;
begin
 inherited Create(true);
end;

function TQuestions.Add(value: TQuestion): Integer;
begin
 Result := inherited Add(value);
end;

function TQuestions.GetItems(Index: Integer): TQuestion;
begin
 Result := TQuestion(inherited Items[Index]);
end;

function TQuestions.IndexOf(value: TQuestion): Integer;
begin
 Result := inherited IndexOf(value);
end;

procedure TQuestions.Insert(Index: Integer; value: TQuestion);
begin
 inherited Insert(Index, value);
end;

function TQuestions.Remove(value: TQuestion): Integer;
begin
 Result := inherited Remove(value);
end;

procedure TQuestions.Delete(Index: Integer);
begin
 inherited Delete(Index);
end;

procedure TQuestions.SetItems(Index: Integer; value: TQuestion);
begin
 inherited Items[Index] := value;
end;

{********** TQUIZ **********}

procedure TQuiz.SetPassword(Value: String);
begin
 if fPassword <> Value then
  fPassword:= Value;
end;

function TQuiz.GetPassword: String;
begin
 result:= fPassword;
end;

procedure TQuiz.SetTitle(Value: String);
begin
 if fTitle <> Value then
  fTitle:= Value;
end;

function TQuiz.GetTitle: String;
begin
 result:= fTitle;
end;

procedure TQuiz.SetType(Value: TQuizType);
begin
 if fType <> Value then
  fType:= Value;
end;

function TQuiz.GetType: TQuizType;
begin
 result:= fType;
end;

procedure TQuiz.SetAuthor(Value: String);
begin
 if fAuthor <> Value then
  fAuthor:= Value;
end;

function TQuiz.GetAuthor: String;
begin
 result:= fAuthor;
end;

procedure TQuiz.SetCreated(Value: TDateTime);
begin
 if fCreated <> Value then
  fCreated:= Value;
end;

function TQuiz.GetCreated: TDateTime;
begin
 result:= fCreated;
end;

procedure TQuiz.SetQuestions(Value: TQuestions);
begin
 fQuestions.Assign(Value);
end;

function TQuiz.GetQuestions: TQuestions;
begin
 result.assign(fQuestions);
end;

constructor TQuiz.Create;
begin
 inherited Create;
 fQuestions:= TQuestions.Create;
 fQuestions.OwnsObjects:= True;
end;

destructor TQuiz.Destroy;
begin
 fQuestions.Free;
 inherited Destroy;
end;

procedure TQuiz.SaveToFile(FileName: String);
 var
 AStr: TFileStream;
 I, J: Integer;
begin
 AStr:= TFileStream.Create(FileName, fmOpenWrite);
 WriteStreamStr(AStr, Encrypt(Title, 5));
 WriteStreamStr(AStr, Title);
 WriteStreamInt(AStr, Integer(QuizType));
 WriteStreamStr(AStr, Author);
 WriteStreamDate(AStr, Created);
 WriteStreamInt(AStr, Questions.Count);
 for I:= 0 to  Questions.Count-1 do
 begin
  WriteStreamStr(AStr, TQuestion(Questions[I]).fQuestion);
  WriteStreamStr(AStr, TQuestion(Questions[I]).fAnswer);
  WriteStreamInt(AStr, TQuestion(Questions[I]).fAnswers.Count);
  for J:= 0 to TQuestion(Questions[I]).fAnswers.Count - 1 do
  begin
   WriteStreamStr(AStr, TQuestion(Questions[I]).fAnswers[J]);
  end;
  WriteStreamBmp(AStr, TQuestion(Questions[I]).fImage);
 end;
 AStr.Free;
end;

procedure TQuiz.LoadFromFile(FileName: String);
 var
 AStr: TFileStream;
 QCount, ACOunt, I: Integer;
 AQuestion: TQuestion;
  J: Integer;
begin
 AStr:= TFileStream.Create(FileName, fmOpenRead);
 Title:= Decrypt(ReadStreamStr(AStr), 5);
 QuizType:= TQuizType(ReadStreamInt(AStr));
 Author:= ReadStreamStr(AStr);
 Created:= ReadStreamDate(AStr);
 QCount:= ReadStreamInt(AStr);
 for I:= 0 to QCount-1 do
 begin
  AQuestion:= TQuestion.Create;
  AQuestion.fQuestion:= ReadStreamStr(AStr);
  AQuestion.fAnswer:= ReadStreamStr(AStr);
  ACount:= ReadStreamInt(AStr);
  for J := 0 to ACount - 1 do
  begin
   AQuestion.fAnswers.Add(ReadStreamStr(AStr));
  end;
  AQuestion.fImage.Assign(ReadStreamBmp(AStr));
  Questions.Add(AQuestion);
 end;
 AStr.Free;
end;

function Encrypt(const S: String; Key: Word): String;
var
  I: byte;
begin
  SetLength(Result, Length(S));
  for I := 1 to Length(S) do
  begin
   Result[I] := char(byte(S[I]) xor (Key shr 8));
   Key := (byte(Result[I]) + Key) * C1 + C2;
  end;
end;

function Decrypt(const S: String; Key: Word): String;
var
  I: byte;
begin
 SetLength(Result, Length(S));
 for I := 1 to Length(S) do
 begin
  Result[I] := char(byte(S[I]) xor (Key shr 8));
  Key := (byte(S[I]) + Key) * C1 + C2;
 end;
end;

procedure WriteStreamStr(Stream : TStream; Str : string);
var
  StrLen : integer;
begin
 StrLen := Length(Str);
 WriteStreamInt(Stream, StrLen);
 Stream.Write(Str[1], StrLen);
end;

function ReadStreamStr(Stream : TStream) : string;
var
  StrLen : integer;
begin
  StrLen := ReadStreamInt(Stream);
  if StrLen > -1 then begin
    SetLength(Result, StrLen);
    Stream.Read(Result[1], StrLen);
    end
  else
    Result := '';
end;

procedure WriteStreamInt(Stream : TStream; Num : integer);
begin
 Stream.Write(Num, SizeOf(Integer));
end;

function ReadStreamInt(Stream : TStream) : integer;
begin
 if Stream.Read(Result, SizeOf(Integer)) < SizeOf(Integer) then
  Result := -1;
end;

procedure WriteStreamDate(Stream : TStream; ADate : TDateTime);
begin
  Stream.Write(ADate, SizeOf(TDateTime));
end;

function ReadStreamDate(Stream : TStream) : TDateTime;
begin
  if Stream.Read(Result, SizeOf(TDateTime)) < SizeOf(TDateTime) then
    Result := -1;
end;

procedure WriteStreamBmp(Stream: TStream; Bitmap: TBitmap);
var
 temp: TMemoryStream;
 size: Integer;
begin
 temp:=TMemoryStream.Create;
 Bitmap.SaveToStream(temp);
 size:=temp.Size;
 Stream.Write(size, SizeOf(Integer));
 Stream.CopyFrom(temp, 0);
 temp.Free;
end;

function ReadStreamBmp(Stream: TStream): TBitmap;
var
 temp: TMemoryStream;
 size: Integer;
begin
 result:=TBitmap.Create;
 temp:=TMemoryStream.Create;
 Stream.Read(size, SizeOf(Integer));
 if (size > 0) then temp.CopyFrom(Stream, size);
  temp.Position:=0;
 result.LoadFromStream(temp);
  temp.Free;
end;

end.

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.