Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

delphi - JPEG data-stream to TImage

I have some image-files stored into one file (some kind of archive). That file looks like this:

enter image description here

Well, it's separated into two segments - header and data-segment. Header (green) contains various info, such as album name, location, date/time, description, number of photos in the album, etc. Data segment (blue & orange) has simple structure and it contains N x JPEG photo. I can extract that "imagedata" segment into new TMemoryStream object and now I want to show it using TImage.

I can use SaveAsFile method of TMemoryStream, set some temporary file-name, load that file from TImage, and later, remove temporary file. That works, but I'm wondering is there any way to send that stream into TImage w/out using temp. files.

Of course, I can write code to extract all those files on hard disk but problem is that I have a lot of archives like this one and just want to write application to read those "albums" instead of having 20,000+ photos on my hard drive.

In short, all I want is to make following procedure works (w/out using temp. files)

procedure ShowImageFromStream(data: TStream; img: TImage);
begin
...
end;

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not sure if that's what you are looking for, but this code should load the JPEG file from stream into the given TImage component:

uses
  JPEG;

procedure ShowImageFromStream(AImage: TImage; AData: TStream);
var
  JPEGImage: TJPEGImage;
begin
  AData.Position := 0;
  JPEGImage := TJPEGImage.Create;
  try
    JPEGImage.LoadFromStream(AData);
    AImage.Picture.Assign(JPEGImage);
  finally
    JPEGImage.Free;
  end;
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...