Solved

JPEG SaveToStream Problem

Posted on 2008-10-05
4
819 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
ID: 22645313
try changing your ms.Position := 0 to:

ms..Seek(0, soFromBeginning);

Give that a try.

John
0
 
LVL 28

Accepted Solution

by:
2266180 earned 500 total points
ID: 22645319
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
ID: 22645320
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
ID: 31503214
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 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

11 Experts available now in Live!

Get 1:1 Help Now