Help! My Code have Error!

coldboy
coldboy used Ask the Experts™
on
I have problem in working with a mini project!
The form is not complex, as the code below:
--------------------------Unit1.pas----------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, ExtDlgs,JPEG;

type
  TForm1 = class(TForm)
    PicOpen: TOpenPictureDialog;
    Image1: TImage;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
Var JPG:tJPEGimage;
  MS:TMemoryStream;
begin
  JPG:=TJpEGimage.Create;
  MS:=TMemorystream.create;
  MS.Clear;
  if PicOpen.execute then
    begin
      JPG.loadfromfile(PicOpen.filename);
      JPG.SaveToStream(MS);
      JPG.LoadFromStream(MS);
      Image1.Picture.Graphic:=JPG;
    end;
  JPG.Free;
  Ms.Free;
end;

end.
---------------------------End of Unit1---------------------------------
But when I run program, After I click BitBtn1, I get this Error Message:
JPEG Error #42
How can I fix this Error?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I would suggest adding this line of code after JPG.SaveToStream(MS):

MS.Position := 0;

SaveToStream will set the position to the end of the stream; consequently, the LoadFromStream call will read zero bytes.
You forgot to rewind the MemoryStream ...

 if PicOpen.execute then
   begin
     JPG.loadfromfile(PicOpen.filename);
     JPG.SaveToStream(MS);
     MS.Seek(0, soFromBeginning); // <----------------
     JPG.LoadFromStream(MS);
     Image1.Picture.Graphic:=JPG;
   end;
gary: sorry, I didn't reloaded the question while testing ....

Author

Commented:
Thank for helping

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial