Mastodon
Programmierung

Streaming JPEG

(Stream Jpeg Jpg LoadFromStream SaveToStream)

The same problem is with TIcon, for example.
Here is a workaround (not very efficient, but it works)
I save size of image binary data before data itself. When loading, I load
data for one image in separate stream, and than load image from this stream.
This method solves two problems:
(1) incorrect stream position after loading some graphics formats;
(2) reading of empty images (empty bitmap saves nothing)

[code lang=”delphi”]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;
[/code]
(I’ve made some changes in code when wrote this letter, so typing errors are
possible)

[tags]Delphi, Graphic[/tags]

0 Kommentare zu “Streaming JPEG

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.