Solved

JPEG SaveToStream Problem

Posted on 2008-10-05
4
826 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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