Solved

JPEG SaveToStream Problem

Posted on 2008-10-05
4
825 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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