DelphiFAQ Home Search:

Streaming out multiple pictures (any raw data)

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Problem:
// After streaming out 2 jpegs, the second one cannot be read
ms := TMemoryStream.Create;
JPG1.SaveToStream(ms);
JPG2.SaveToStream(ms);

ms.Seek(0,0);
JPG1.LoadFromStream(ms);
// now it is at the end of the stream
// and JPG2 won't be read

JPG2.LoadFromStream(ms);

Solution:
Store the length of each graphic (raw data blob) before the data.
Use this value when reading from the stream.

procedure SavePicture(Stream: TStream; gr: TGraphic);
var
  P,
  newp: integer;
begin
  P := Stream.Position;
  Stream.WriteBuffer(P, sizeof(P));
  gr.SaveToStream(Stream);
  newp := Stream.Position;
  Stream.Position := P;
  P := newp-P-sizeof(P);
  Stream.WriteBuffer(P, sizeof(P));
  Stream.Position := newp;
end;


procedure LoadPicture(Stream: TStream; gr: TGraphic);
var
  MemStream: TMemoryStream;
  v        : integer;
begin 
  MemStream := TMemoryStream.Create; 
  try 
    Stream.ReadBuffer(v, sizeof(v)); 
    MemStream.SetSize(v); 
    Stream.ReadBuffer((MemStream.Memory)^, v); 
    MemStream.Position := 0; 
    gr.LoadFromStream(MemStream); 
  finally 
    MemStream.Free; 
  end; 
end; 
You don't like the formatting? Check out SourceCoder then!
Content-type: text/html

Comments:

2008-08-05, 20:39:14
anonymous  
know this pic?


Keywords:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: