[Solved] [lNet] How can I download an image to a stream? lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Jishaxe · 2012-03-15 19:27 Hello, I am using lNet and I was wondering how I could download an image with HTTP (that I know the URL to) to a stream so I can edit it and save it to disk? Is there an easier way to do what I want to achieve? Would an image be classed as a binary file, just so I get my definitions right? Here is what I have so far:  Code: [Select]unit mainunit; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,   ComCtrls, StdCtrls, lNetComponents, lhttp, lhttputil; type   { TMainForm }   TMainForm = class(TForm)     StartButton: TButton;     CloseButton: TButton;     HTTPClient: TLHTTPClientComponent;     StatusBar: TStatusBar;     procedure HTTPClientDoneInput(ASocket: TLHTTPClientSocket);     function HTTPClientInput(ASocket: TLHTTPClientSocket; ABuffer: pchar;       ASize: integer): integer;     procedure StartButtonClick(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   MainForm: TMainForm;   JPGStream: TStream;   JPGImage: TJpegImage; const   ImageLoc: String = 'http://www.enigmagroup.org/missions/programming/3/image.php';   SaveTo: String = 'tempimage.jpg'; implementation {$R *.lfm} { TMainForm } function TMainForm.HTTPClientInput(ASocket: TLHTTPClientSocket; ABuffer: pchar;   ASize: integer): integer; begin   JPGStream.WriteBuffer(ABuffer^, ASize); end; procedure TMainForm.HTTPClientDoneInput(ASocket: TLHTTPClientSocket); var   SaveStream: TFileStream; begin   ASocket.Disconnect;   JPGImage := TJpegImage.Create;   JPGStream.Position := 0;   JPGImage.LoadFromStream(JPGStream);   SaveStream := TFileStream.Create(SaveTo,fmOpenWrite or fmCreate);   JPGImage.SaveToStream(SaveStream);   ShowMessage('Done!');   JPGStream.Free;   SaveStream.Free; end; procedure TMainForm.StartButtonClick(Sender: TObject); var   aHost, aURI: string;   aPort: Word; begin   JPGStream := TStream.Create;   DecomposeURL(ImageLoc, aHost, aURI, aPort);   HTTPClient.Host := aHost;   HTTPClient.URI  := aURI;   HTTPClient.Port := aPort;   HTTPClient.SendRequest; end; end. It's basically bad code stuck together. It looks like I can't just use a TStream as illustrated here: http://www.lazarus.freepascal.org/index.php?topic=8339.0 Because it won't let me call Write on it. Please talk with simple language, I don't know much about this. What stream would I use for this? Oh, and I would like for now to stick with lNet as long as what I am trying to do is possible with it. Thanks ---------------------------------------------------------------------- Jishaxe · 2012-03-15 19:49 Solved. Well, programmatically. Using a TMemoryStream works fine but it needs to be the image directly instead of inside a PHP script... I don't know how I'm going to get that.