Solved

JPEG SaveToStream Problem

Posted on 2008-10-05
4
817 Views
Last Modified: 2013-11-05
Can anyone please tell me why this doesn't work.  As a test, after I called LoadFromFile I also loaded the image into a TImage component and it appears just fine.  MyJPEG height and width are correct.  It just doesn't load anything to stream.  Aargh!   Thanks.

var
  ms: TMemoryStream;
  MyJPEG: TJPEGImage;
begin
  ms := TMemoryStream.Create;
  try
    MyJPEG := TJPEGImage.Create;
    try
        MyJPEG.LoadFromFile('image1.jpg');
        MyJPEG.SaveToStream(ms);
        ms.Position := 0;
//ms.memory^ shows "(no value)" and if I try to LoadFromStream() into any image the stream is empty.
    finally
       MyJPEG.Free;
       end;
  finally
     ms.Free;
     end;


0
Comment
Question by:alexiat
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Johnjces
Comment Utility
try changing your ms.Position := 0 to:

ms..Seek(0, soFromBeginning);

Give that a try.

John
0
 
LVL 28

Accepted Solution

by:
ciuly earned 500 total points
Comment Utility
don't blindly trust the debugger. different debugger versions (from different delphi versions) have different small and rare issues with debugging sertain more complex expressions.

instead, use something that you can really rely on. like
ms.savetofile(filename here);

your code works perfectly with this.
uses jpeg;
 

procedure TForm1.FormCreate(Sender: TObject);

var

  ms: TMemoryStream;

  MyJPEG: TJPEGImage;

begin

  ms := TMemoryStream.Create;

  try

    MyJPEG := TJPEGImage.Create;

    try

      MyJPEG.LoadFromFile('d:\test.jpg');

      MyJPEG.SaveToStream(ms);

      ms.Position := 0;

      ms.savetofile('d:\test_mem.jpg');

    finally

      MyJPEG.Free;

    end;

  finally

    ms.Free;

  end;

end;

Open in new window

0
 
LVL 18

Expert Comment

by:Johnjces
Comment Utility
ms.Seek(0, soFromBeginning).

I double clicked the dot... only one not ms.. but ms.

(I know you know that but I messed up so want to make certain).

John
0
 

Author Closing Comment

by:alexiat
Comment Utility
Whew. Thanks. Who knew you couldn't trust the debugger.  I guess this means it is the 3rd party component who is trying to use the stream that is having problems with it.  I thought I was blindly missing something.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now