[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Streaming Objects to File ?

Posted on 2010-03-26
7
Medium Priority
?
415 Views
Last Modified: 2012-05-09
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

0
Comment
Question by:DelphiRulez
  • 4
  • 2
7 Comments
 
LVL 7

Assisted Solution

by:ebob42
ebob42 earned 400 total points
ID: 28689217
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
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 28689294
For the Q1
property Items[Index: Integer]
should be
property Items(Index: Integer)
0
 

Author Comment

by:DelphiRulez
ID: 28690823
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
LVL 7

Expert Comment

by:ebob42
ID: 28690974
I would use FileStream, or a plan TextFile that you can read and write as well to.
0
 

Author Comment

by:DelphiRulez
ID: 28692198
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
 

Author Comment

by:DelphiRulez
ID: 28694062
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
 

Accepted Solution

by:
DelphiRulez earned 0 total points
ID: 28708432
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question