Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: TJPEGImage.Assign does not work??


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Sep 11, 2015 11:57 AM Last Post By: Guest
Guest
TJPEGImage.Assign does not work??
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2015 6:26 AM
A am stumped by this.

I read a JPEGImage from a Stream. The Stream has a valid JPeg image it it.
After the assign, the JPGImage is empty. The Width and Height are zero. SafeToDisk gives an empty file.
However, when I do a Stream.SaveToDisk, the Image is saved to disk fine, and I can open it with any program.

What am I doing wrong??

//...
	// AStream is the primary incoming Stream
	ASize := Reader.ReadInteger;
           
      if ASize > 0 then
      begin
        BinStream := TMemoryStream.Create;
        Reader.FlushBuffer; // Linked to the ASTream
        try
          BinStream.CopyFrom(AStream, ASize); // ASize is valid size
                 
            ASARImage.FJPGImage := TJPEGImage.Create;
            ASARImage.FJPGImage.LoadFromStream(BinStream);
            // At this point, Width and Height are zero
           
            ASARImage.FJPGImage.SaveToFile('c:\temp\jpegtest.jpg'); // This file is empty
            BinStream.SaveToFile('c:\temp\binarytest.jpg'); // This is a valid JPEG image
         except
         end;  
 
        finally
          BinStream.Free;
        end;

--
Bart Kindt, CEO and developer
SARTrack Limited New Zealand
http://www.sartrack.co.nz/

Carl-Henrik Nil...

Posts: 53
Registered: 3/26/02
Re: TJPEGImage.Assign does not work??  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2015 7:08 AM   in response to: Guest in response to: Guest
Bart Kindt wrote:
A am stumped by this.

I read a JPEGImage from a Stream. The Stream has a valid JPeg image it it.
After the assign, the JPGImage is empty. The Width and Height are zero. SafeToDisk gives an empty file.
However, when I do a Stream.SaveToDisk, the Image is saved to disk fine, and I can open it with any program.

What am I doing wrong??

//...
	// AStream is the primary incoming Stream
	ASize := Reader.ReadInteger;
           
      if ASize > 0 then
      begin
        BinStream := TMemoryStream.Create;
        Reader.FlushBuffer; // Linked to the ASTream
        try
          BinStream.CopyFrom(AStream, ASize); // ASize is valid size
                 
            ASARImage.FJPGImage := TJPEGImage.Create;
            ASARImage.FJPGImage.LoadFromStream(BinStream);
            // At this point, Width and Height are zero
           
            ASARImage.FJPGImage.SaveToFile('c:\temp\jpegtest.jpg'); // This file is empty
            BinStream.SaveToFile('c:\temp\binarytest.jpg'); // This is a valid JPEG image
         except
         end;  
 
        finally
          BinStream.Free;
        end;

You haven't set the position of the BinStream to 0 before you do ASARImage.FJPGImage.LoadFromStream(BinStream);
--
C-H
Guest
Re: TJPEGImage.Assign does not work??
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2015 7:10 AM   in response to: Carl-Henrik Nil... in response to: Carl-Henrik Nil...
Carl-Henrik Nilsson wrote:
You haven't set the position of the BinStream to 0 before you do ASARImage.FJPGImage.LoadFromStream(BinStream);

I have just Created it, it is 0 by default.

--
Bart Kindt, CEO and developer
SARTrack Limited New Zealand
http://www.sartrack.co.nz/
Carl-Henrik Nil...

Posts: 53
Registered: 3/26/02
Re: TJPEGImage.Assign does not work??  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2015 7:15 AM   in response to: Guest in response to: Guest
Bart Kindt wrote:
Carl-Henrik Nilsson wrote:
You haven't set the position of the BinStream to 0 before you do ASARImage.FJPGImage.LoadFromStream(BinStream);

I have just Created it, it is 0 by default.

But you did BinStream.CopyFrom(AStream, ASize); // ASize is valid size
so the position is ASize, or?
C-H
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: TJPEGImage.Assign does not work??  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2015 11:27 AM   in response to: Guest in response to: Guest
Bart wrote:

I have just Created it, it is 0 by default.

Sure, when you create it. But once you call CopyFrom() to populate the stream
with data, its Position is no longer 0, it is at the end of the stream, ie
the same value as its Size. So when you then call LoadFromStream(), there
is no data for it to read (Size minus Position is 0). So you must reset
the Position back to 0 before calling LoadFromStream().

BinStream.CopyFrom(AStream, ASize);
BinStream.Position := 0; // <-- add this
ASARImage.FJPGImage.LoadFromStream(BinStream);


The TMemoryStream.SaveTo...() methods ignore the Position, they save the
entire stream data from beginning to end. That is why SaveToFile() works
regardless of the Position.

--
Remy Lebeau (TeamB)
Guest
Re: TJPEGImage.Assign does not work??
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2015 11:57 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

Ah, bugger. Yes.

Many thanks, Remy and Carl-Henrik.

--
Bart Kindt, CEO and developer
SARTrack Limited New Zealand
http://www.sartrack.co.nz/
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02